Skip to content
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

Be stricter about access to generic vars from class #18100

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Nov 3, 2024

This behaviour was intentionally chosen in #6418

I agree with Ivan's comment there that it's similar to how mypy allows instantiation of type[Abstract] -- but that's a thing that I want to explore disallowing.

Let's see primer!

This behaviour was intentionally chosen in python#6418

I agree with Ivan's comment there that it's similar to how mypy allows
instantiation of `type[Abstract]` -- but that's a thing that I want to
explore disallowing.

Let's see primer

This comment has been minimized.

@hauntsaninja
Copy link
Collaborator Author

prefect is self type, altair is self type (singleton), artigraph is runtime typing i'm fine with, pytest is abstract generic class with only concrete subclasses, pandera is also fine, steam is self type, homeassistant is self type

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

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

artigraph (https://github.com/artigraph/artigraph)
+ src/arti/internal/mappings.py:246: error: Access to generic class variables is ambiguous  [misc]
+ src/arti/storage/__init__.py:90: error: Access to generic class variables is ambiguous  [misc]
+ src/arti/storage/__init__.py:105: error: Access to generic class variables is ambiguous  [misc]
+ src/arti/storage/__init__.py:115: error: Access to generic class variables is ambiguous  [misc]

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/capture.py:913: error: Access to generic instance variables via class is ambiguous  [misc]
+ src/_pytest/capture.py:914: error: Access to generic instance variables via class is ambiguous  [misc]
+ src/_pytest/capture.py:946: error: Access to generic instance variables via class is ambiguous  [misc]
+ src/_pytest/capture.py:947: error: Access to generic instance variables via class is ambiguous  [misc]

steam.py (https://github.com/Gobot1234/steam.py)
- steam/state.py:3026: error: Access to generic instance variables via class is ambiguous  [misc]
- steam/state.py:3033: error: Access to generic instance variables via class is ambiguous  [misc]
- steam/state.py:3035: error: Access to generic instance variables via class is ambiguous  [misc]
+ steam/ext/commands/converters.py:239: error: Access to generic instance variables via class is ambiguous  [misc]

pandera (https://github.com/pandera-dev/pandera)
+ pandera/api/dataframe/model.py:262: error: Access to generic instance variables via class is ambiguous  [misc]
- pandera/api/dataframe/model.py:264: error: Unused "type: ignore" comment  [unused-ignore]
- pandera/api/dataframe/model.py:265: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/components/yeelight/scanner.py:54: error: Incompatible types in assignment (expression has type "YeelightScanner", variable has type "Self | None")  [assignment]
+ homeassistant/components/yeelight/scanner.py:55: error: Incompatible return value type (got "Self | None", expected "YeelightScanner")  [return-value]

@cdce8p
Copy link
Collaborator

cdce8p commented Dec 28, 2024

While debugging the Home Assistant error, I noticed that Self without ClassVar is currently interpreted as Any within the singleton pattern thus masking the issue. Might be worth adding a test case for it to make sure it isn't Any.

from typing import ClassVar, Self

class X:
    _inst: Self | None = None

    @classmethod
    def func(cls) -> Self:
        reveal_type(cls._inst)  # Any | None
        if cls._inst is None:
            cls._inst = cls()
        reveal_type(cls._inst)  # Any
        return cls._inst
class Y:
    _inst: ClassVar[Self | None] = None

    @classmethod
    def func(cls) -> Self:
        reveal_type(cls._inst)  # Self | None
        if cls._inst is None:
            cls._inst = cls()
        reveal_type(cls._inst)  # Self
        return cls._inst

Edit: Fix for Home Assistant: home-assistant/core#134135

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.

2 participants