`__getattribute__` return type is not reflected in the type of attributes
Describe the bug
When a class overrides __getattribute__ with a specific return type signature R, all attribute accesses to instances of that class should also have type R.
Code or Screenshots
from typing import TYPE_CHECKING
class FloatContainer:
a = 1
b = 2
c = 3
def __getattribute__(self, name: str) -> float:
# Converts any attribute to float before returning it
return float(super().__getattribute__(name))
if TYPE_CHECKING:
reveal_type(FloatContainer().a)
print(type(FloatContainer().a))
Pyright says
information: Type of "FloatContainer().a" is "int"
Python says:
<class 'float'>
Version
uv run pyright --version
pyright 1.1.414
Other
I don't think this behaviour is discussed in the typing spec, but I would expect it to be implicit from the data model spec which says __getattribute__ is
Called unconditionally to implement attribute accesses for instances of the class. [...] This method should return the (computed) attribute value or raise an AttributeError exception.
Happy to raise this issue as a clarification of the spec in https://discuss.python.org. I can't find any existing threads about this: https://discuss.python.org/search?expanded=true&q=getattribute%20%23typing
Overriding __getattribute__ like that is unsafe, IMO. The method is for instance attribute lookup, and vars(obj) is supposed to return the instance attribute dict obj.__dict__. In your example, vars(FloatContainer()) calls float(super().__getattribute__("__dict__")), resulting in a TypeError:
TypeError: float() argument must be a string or a number, not 'dict'
FloatContainer().__class__ would raise similarly.
isinstanceuses__class__as a fallback whentype(...)fails (type(...)is C implementation lookup on the object'sob_typepointer). Soisinstance(FloatContainer(), int)raises too.- Even if
"__class__"were special-cased in__getattribute__to returntype[Self], that would invalidate the return typefloat.
Regardless, Pyright only checks __getattribute__ after checking class members. Whether that's correct or not is probably a spec concern, since checking before would imply that __class__ and __dict__ are permitted to be degenerate types.
Preserving the dunder methods in the __getattribute__() implementation is pretty trivial, and you could prove that to the type checker using @overload. However, it would be quite ugly boilerplate and I would probably rather that the spec allow the type checker to just ignore anything that overrides the type of a special dunder method.
you could prove that to the type checker using
@overload
from typing import Any, Literal, Self, overload
class FloatContainer:
a = 1
@overload
def __getattribute__(self, name: Literal["__class__"], /) -> type[Self]: ...
@overload
def __getattribute__(self, name: Literal["__dict__"], /) -> dict[str, Any]: ...
@overload
def __getattribute__(self, name: str, /) -> float: ...
def __getattribute__(self, name: str, /) -> object:
attr = super().__getattribute__(name)
if name in {"__class__", "__dict__"}:
return attr
return float(attr)
reveal_type(FloatContainer().a)
reveal_type(FloatContainer().__class__)
reveal_type(FloatContainer().__dict__)
The current main reports an overlapping overload here, since Literal["__class__"] and Literal["__dict__"] are subtypes of str, but the return types are not subtypes of float. Pyright resolves the actual names as descriptors on object, so the overloads just expose the incompatible return type:
7:9 - error: Overload 1 for "__getattribute__" overlaps overload 3 and returns an incompatible type (reportOverlappingOverload)
18:13 - information: Type of "FloatContainer().a" is "int"
19:13 - information: Type of "FloatContainer().__class__" is "type[FloatContainer]"
20:13 - information: Type of "FloatContainer().__dict__" is "dict[str, Any]"