201 questions
1 vote
1 answer
149 views
How to implement Python's unified attribute lookup (__getattribute__) in C++, handling descriptors and metaclasses?
I'm developing a Python interpreter from scratch in C++ as a hobby project to deepen my understanding of the language's internals. I'm currently stuck on implementing the attribute lookup mechanism (...
2 votes
3 answers
180 views
How to correctly type-annotate a classmethod-like descriptor in python?
classonlymethod decorator/descriptor defined below is like the built-in classmethod, but you're not allowed to call the method on instances, only on the class itself. from typing import Concatenate, ...
1 vote
1 answer
76 views
Exception thrown in python magic method is lost on the way
For a project, I want to prevent the use of certain methods in an overriding class. As this happens rather frequently, I am using a metaclass to block many methods at once (how and why is not within ...
1 vote
0 answers
70 views
Can I override type of an existing attribute by `__getattribute__` method?
I wrote a simple code where I override the __getattribute__ method so that it always returns an empty string. But Pyright doesn't understand this and complains "int" is not assignable to &...
1 vote
1 answer
128 views
How to create different type for class variable and instance variable
I want to explain to Pyright that my variables in class and instance have different types. I managed to overload __get__ method to achieve this, but now Pyright complains about initialization of ...
2 votes
1 answer
69 views
How to pass any object as a classmethod's first argument - circumvent injection of class argument
Passing a class, or a totally different object, as the first argument to method is easy: class Foo: def method(self): ... Foo.method(object()) # pass anything to self I wonder, is this possible ...
1 vote
1 answer
82 views
How to check if an object is a method_descriptor
Given an object, how can I make a check if it is a method_descriptor? is_method_descriptor = isinstance(obj, method_descriptor) # does not work The problem: method_descriptor is a builtin but not an ...
1 vote
1 answer
85 views
Python descriptors on Readonly attributes
I want to refactor a big part of my code into a generic descriptor for read only attribute access. The following is an example of property based implementation class A: def __init__(self, n): ...
1 vote
1 answer
78 views
Dynamic read-only attributes in python instances
EDIT This code contains several bugs, see jsbueno's answer below for a correct version I would like to create read-only attributes that dynamically retrieve values from an internal dictionary. I have ...
1 vote
0 answers
166 views
Descriptor type hints
I'm trying to use type hints in my code but I simply don't understand how the concept applies to a Python descriptor. The only relevant info I could find on the matter was this 6+ year old post but I ...
2 votes
3 answers
197 views
Properties vs. generic setter/getter and descriptor
I can't seem to find a definitive answer on the matter and I guess the reason is because it depends on the situation. a, b and c (and d, e, f... as only 3 attributes are listed in this example for ...
0 votes
0 answers
16 views
Descriptor as decorator with variable arguments [duplicate]
I want to create a @property like feature with serialization capabilities. It would take some arguments. I have created a descriptor like so: class CustomMethod: def __init__(self, serialize: bool ...
0 votes
0 answers
46 views
How to track access to class variables in a custom ORM using Python descriptors?
I'm building my own ORM in Python and ran into an issue. I want to track the access to class variables in order to know which ForeignKey is being used in each part of my code. class IQuery(ABC): &...
0 votes
2 answers
90 views
Descriptor in DataClass
Here is descriptor class: #descriptor class Validator: def __set_name__(self,owner,name): self.private_name = '_' + name def __set__(self,obj,value): self.validate(value) ...
1 vote
1 answer
56 views
Decorate instance methods with decorator class that replaces method with class instance
I would like to be able to decorate instance methods with a class Step such that the methods are replaced by a Step object. At the same time, I'd like to have the option be able to instantiate a step ...