Disallow calling type on Protocol#18095
Conversation
3d3a3be to
afa72b9
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The websockets error is an interesting one: elif isinstance(message, AsyncIterable):
... type(message).__aiter__ ...This is kind of reasonable, since modules can't really implement Maybe we could infer that protocols with certain dunder methods can only be implemented by regular class instances, and fall back to the to the old |
| ret_type=UnionType( | ||
| [ | ||
| self.named_type("builtins.type"), | ||
| self.named_type("types.ModuleType"), |
There was a problem hiding this comment.
(sorry, I got confused myself, deleted & reposted)
self.named_type('types.ModuleType') isn't the correct type here:
import types
assert type(types) is types.ModuleTypeWhen you call type(some_module), you obtain types.ModuleType class itself, not its instance. So this union isn't type | types.ModuleType | Any, it should be type | type[ModuleType] | Any, and at this point type[ModuleType] member is redundant (it's also a type, isn't it?).
2dc29e6 to
8792bd6
Compare
|
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/pydantic/pydantic)
+ pydantic/dataclasses.py:266: error: "type" has no attribute "__pydantic_validator__" [attr-defined]
scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/python.py:268: error: Unused "type: ignore" comment [unused-ignore]
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1155: 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:1155: error: No overload variant of "builds" of "BuildsFn" matches argument types "type", "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]
websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/protocol.py:682: error: "type" has no attribute "__aiter__" (not async iterable) [attr-defined]
+ src/websockets/legacy/protocol.py:689: error: "type" has no attribute "__anext__" [attr-defined]
|
Fixes #16919, fixes #16890
It's possible we should not issue the diagnostic and only return object, but that may confuse users. Let's see the primer.Should also probably disallow
type[P]as well.