Repository navigation
Be stricter about access to generic vars from class - #18100
Draft
hauntsaninja wants to merge 5 commits into
Draft
hauntsaninja wants to merge 5 commits into
hauntsaninja wants to merge 5 commits into
Conversation
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.
This comment has been minimized.
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 |
hauntsaninja
force-pushed
the
generic
branch
from
December 28, 2024 03:10
a5e2ad7 to
26618b0
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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]
|
Collaborator
|
While debugging the Home Assistant error, I noticed that 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._instclass 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._instEdit: Fix for Home Assistant: home-assistant/core#134135 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!