-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
memoryview
: remove inheritance from Sequence
#12781
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
Conversation
index
and count
method
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Given how little mypy_primer fallout there is, I think we should probably go ahead with this! Rather than inventing a fake base class, though, could you just have memoryview inherit from Generic[_I]
? It seems like the only method it inherits from its current superclasses without overriding the method is __reversed__
, and it looks to me like it doesn't have a __reversed__
method at runtime either:
>>> memoryview.__reversed__
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
memoryview.__reversed__
AttributeError: type object 'memoryview' has no attribute '__reversed__'
So I don't think the __memory_view
class you added for it to inherit from should make any difference in practical terms
|
Yes. If you look at the stub, Lines 1707 to 1714 in c76c3e7
|
thanks for your answer |
No problem! Thanks for the PR :D |
Diff from mypy_primer, showing the effect of this PR on open source code: hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1137: error: No overload variant of "builds" of "BuildsFn" matches argument types "type[DataclassInstance]", "dict[str, int | float | Path | DataClass_ | type[DataClass_] | ListConfig | DictConfig | Enum | Sequence[HydraSupportedType] | Mapping[Any, HydraSupportedType] | None]", "bool | None", "Literal['none', 'partial', 'all', 'object'] | None", "DataclassOptions" [call-overload]
+ src/hydra_zen/structured_configs/_implementations.py:1137: error: No overload variant of "builds" of "BuildsFn" matches argument types "type[DataclassInstance]", "dict[str, int | float | memoryview[int] | Path | DataClass_ | <6 more items> | None]", "bool | None", "Literal['none', 'partial', 'all', 'object'] | None", "DataclassOptions" [call-overload]
- src/hydra_zen/structured_configs/_implementations.py:2666: error: Incompatible types in assignment (expression has type "tuple[int | float | Path | DataClass_ | type[DataClass_] | ListConfig | DictConfig | Enum | Sequence[HydraSupportedType] | Mapping[Any, HydraSupportedType] | None, ...]", variable has type "list[Importable | Callable[P, R] | type[Builds[Importable]] | type[BuildsWithSig[Importable, Any]] | type[BuildsWithSig[type[R], P]] | Any]") [assignment]
+ src/hydra_zen/structured_configs/_implementations.py:2666: error: Incompatible types in assignment (expression has type "tuple[int | float | memoryview[int] | Path | DataClass_ | <6 more items> | None, ...]", variable has type "list[Importable | Callable[P, R] | type[Builds[Importable]] | type[BuildsWithSig[Importable, Any]] | type[BuildsWithSig[type[R], P]] | Any]") [assignment]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
index
and count
methodmemoryview
: remove inheritance from Sequence
This seems wrong:
It may not implement the interface correctly, but it is registered as a subclass. I think we should reflect that subclass relationship in typeshed. |
I agree, but hot to accomplish this? is it possible to exclude some method from base classes? |
Ah, I should have checked that but forgot to; sorry. In this specific case I still think this change is a net improvement, given that:
IMO we should take the issue of whether builtins should inherit from generic collections on a case-by-case basis, and in this case the benefits of the change seemed to clearly outweigh the costs. But I also don't feel too strongly if you want to revert. Another solution might be to put |
Is the mypy_primer trustworthy? Haven't yet thought it through but mypy still has the promotions behaviour and bytes is a Sequence |
Oh, good question! Looks like pyright removed the promotion behaviour in microsoft/pyright#8835, so I suppose a primer run using pyright would be a better judge here. |
Using AlexWaygood/pyright#1 for a primer run using pyright... |
The mypy_primer report with pyright indicates it only adds one error in the checked projects, and the error occurs inside a |
Doesn't pyright primer run on fewer projects? Intuitively I feel it's much likelier that someone is relying on memoryview being a Sequence than that they're relying on I think we should also fix something in CPython here at runtime, though: either remove the Sequence inheritance from memoryview or add the missing methods at runtime. |
@hauntsaninja obviously knows more than me on that question! I only see 9 matches for
well, but with the stubs prior to this, I imagine IDEs would be e.g. suggesting
Yes, that would obviously be the ideal solution. I think it's pretty unlikely we'll be able to remove the ABC registration at runtime, though, for backwards-compatibility reasons, so adding the methods would probably be the only viable solution in CPython, I think. |
I massively expanded the set of projects pyright runs on at some point this year. There are still differences in coverage, but none that really matter to us here (many fewer projects have pyright configuration, we don't necessarily check the exact same set of files, etc) |
This reverts commit f625e92.
Fixes #12780