Skip to content

Cleanup generic class variable access #19292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Jun 13, 2025

Fixes #5144
Fixes #15223

This is related to the work on #7724

Now that all attribute access goes through checkmember.py there is not much benefit in giving an error at the definition site, especially that it prohibits some valid (and common) use cases, see comments in #5144. While looking at this I discovered a bunch of defects in the implementation, that I also fix (I am keeping unsafe self-type related logic as is):

  • We used to erase type vars of the definition class instead of the use class. This caused type variables leaks.
  • The erasure was inconsistent, so that in some cases we silently erased type variables to Any even in allowed use cases
  • TypeVarTuple and ParamSpec were not handled as equal to regular type variables (I guess because of old problems with erasing them)

@ilevkivskyi ilevkivskyi requested a review from JukkaL June 13, 2025 15:56

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis)
- ibis/common/bases.py:110: error: Unsupported target for indexed assignment ("Mapping[Any, Any]")  [index]
+ ibis/common/bases.py:110: error: Unsupported target for indexed assignment ("Mapping[Any, Self]")  [index]

hydpy (https://github.com/hydpy-dev/hydpy)
+ hydpy/core/variabletools.py:2448: error: Unused "type: ignore" comment  [unused-ignore]

artigraph (https://github.com/artigraph/artigraph)
- src/arti/internal/mappings.py:100: error: ClassVar cannot contain type variables  [misc]
+ src/arti/storage/__init__.py:74: error: Unused "type: ignore" comment  [unused-ignore]

comtypes (https://github.com/enthought/comtypes)
+ comtypes/hints.pyi:56: error: Unused "type: ignore" comment  [unused-ignore]

pwndbg (https://github.com/pwndbg/pwndbg)
- pwndbg/aglib/heap/structs.py:175: error: Access to generic instance variables via class is ambiguous  [misc]
- pwndbg/aglib/heap/structs.py:206: error: Access to generic instance variables via class is ambiguous  [misc]

@ilevkivskyi
Copy link
Member Author

mypy_primer looks good.

x: Tuple[Unpack[Ts]]

reveal_type(C.x) # E: Access to generic instance variables via class is ambiguous \
# N: Revealed type is "builtins.tuple[Any, ...]"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test also defining a subclass and then accessing the attribute via the subclass, and an instance of the subclass?

[case testSelfTypeClassMethodNotSilentlyErased]
from typing import ClassVar, Self, Optional

class X:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test subclassing and accessing _inst outside the class body?

x: T
y: ClassVar[T]

class C(B[T]): ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to also test attribute access in cases like class D(B[int]): .... Should D.y have type int then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@classmethod forces Self to coerce to a specific type Generic ClassVar Type failing
2 participants