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

Infer correct types with overloads of Type[Guard | Is] #17678

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sobolevn
Copy link
Member

Closes #17579

Consider this as a prototype, because I understand that there might be a lot of extra work to get this right.

However, this does solve this problem in the original issue.

Copy link
Contributor

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

pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/json.py:80: error: Argument 1 to "asdict" has incompatible type "type[DataclassInstance] | DataclassInstance"; expected "DataclassInstance"  [arg-type]
- pydantic/v1/json.py:80: error: No overload variant of "asdict" matches argument type "type[DataclassInstance]"  [call-overload]
- pydantic/v1/json.py:80: note: Possible overload variants:
- pydantic/v1/json.py:80: note:     def asdict(obj: DataclassInstance) -> dict[str, Any]
- pydantic/v1/json.py:80: note:     def [_T] asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, Any]]], _T]) -> _T

koda-validate (https://github.com/keithasaurus/koda-validate)
+ koda_validate/typehints.py:119: error: Argument 1 to "DataclassValidator" has incompatible type "type[DataclassInstance] | DataclassInstance"; expected "type[Any]"  [arg-type]
+ koda_validate/signature.py:81: error: Argument 1 to "DataclassValidator" has incompatible type "type[DataclassInstance] | DataclassInstance"; expected "type[Any]"  [arg-type]
+ koda_validate/signature.py:81: error: Argument 1 to "dataclass_no_coerce" has incompatible type "type[DataclassInstance] | DataclassInstance"; expected "type[DataclassInstance]"  [arg-type]

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/_io/pprint.py:114: error: Unused "type: ignore" comment  [unused-ignore]
+ src/_pytest/_io/pprint.py:116: error: "DataclassInstance" has no attribute "__dataclass_params__"  [attr-defined]
+ src/_pytest/_io/pprint.py:122: error: Unused "type: ignore" comment  [unused-ignore]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ui/select.py:1072: error: "Callable[[V, Interaction[Any], BaseSelectT], Coroutine[Any, Any, Any]]" has no attribute "__discord_ui_model_type__"  [attr-defined]
+ discord/ui/select.py:1072: error: Item "function" of "Callable[[V, Interaction[Any], BaseSelectT], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]" has no attribute "__discord_ui_model_type__"  [union-attr]
- discord/ui/select.py:1073: error: "Callable[[V, Interaction[Any], BaseSelectT], Coroutine[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [attr-defined]
+ discord/ui/select.py:1073: error: Item "function" of "Callable[[V, Interaction[Any], BaseSelectT], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [union-attr]
- discord/ui/select.py:1082: error: "Callable[[V, Interaction[Any], BaseSelectT], Coroutine[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [attr-defined]
+ discord/ui/select.py:1082: error: Item "function" of "Callable[[V, Interaction[Any], BaseSelectT], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [union-attr]
- discord/ui/select.py:1084: error: "Callable[[V, Interaction[Any], BaseSelectT], Coroutine[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [attr-defined]
+ discord/ui/select.py:1084: error: Item "function" of "Callable[[V, Interaction[Any], BaseSelectT], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [union-attr]
- discord/ui/select.py:1100: error: "Callable[[V, Interaction[Any], BaseSelectT], Coroutine[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [attr-defined]
+ discord/ui/select.py:1100: error: Item "function" of "Callable[[V, Interaction[Any], BaseSelectT], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]" has no attribute "__discord_ui_model_kwargs__"  [union-attr]
+ discord/ext/tasks/__init__.py:626: error: Incompatible return value type (got "Callable[[Any, BaseException], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]", expected "ET")  [return-value]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1130: note:     def [R, P`3934] builds(cls, type[BuildsWithSig[type[R], P]], /, *, zen_partial: Literal[False] | None = ..., populate_full_signature: Literal[True], zen_wrappers: Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None | Sequence[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None] = ..., zen_meta: Mapping[str, SupportedPrimitive] | None = ..., hydra_recursive: bool | None = ..., hydra_convert: Literal['none', 'partial', 'all', 'object'] | None = ..., hydra_defaults: list[str | DataClass_ | type[DataClass_] | Mapping[str, str | Sequence[str] | None]] | None = ..., dataclass_name: str | None = ..., builds_bases: tuple[()] = ..., zen_dataclass: DataclassOptions | None = ..., frozen: bool = ..., zen_convert: ZenConvert | None = ...) -> type[BuildsWithSig[type[R], P]]
+ src/hydra_zen/structured_configs/_implementations.py:1130: note:     def [R, P`3937] builds(cls, type[BuildsWithSig[type[R], P]], /, *, zen_partial: Literal[False] | None = ..., populate_full_signature: Literal[True], zen_wrappers: Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None | Sequence[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None] = ..., zen_meta: Mapping[str, SupportedPrimitive] | None = ..., hydra_recursive: bool | None = ..., hydra_convert: Literal['none', 'partial', 'all', 'object'] | None = ..., hydra_defaults: list[str | DataClass_ | type[DataClass_] | Mapping[str, str | Sequence[str] | None]] | None = ..., dataclass_name: str | None = ..., builds_bases: tuple[()] = ..., zen_dataclass: DataclassOptions | None = ..., frozen: bool = ..., zen_convert: ZenConvert | None = ...) -> type[BuildsWithSig[type[R], P]]
- src/hydra_zen/structured_configs/_implementations.py:1130: note:     def [P`3936, R] builds(cls, Callable[P, R], /, *, zen_partial: Literal[False] | None = ..., populate_full_signature: Literal[True], zen_wrappers: Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None | Sequence[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None] = ..., zen_meta: Mapping[str, SupportedPrimitive] | None = ..., hydra_recursive: bool | None = ..., hydra_convert: Literal['none', 'partial', 'all', 'object'] | None = ..., hydra_defaults: list[str | DataClass_ | type[DataClass_] | Mapping[str, str | Sequence[str] | None]] | None = ..., dataclass_name: str | None = ..., builds_bases: tuple[()] = ..., zen_dataclass: DataclassOptions | None = ..., frozen: bool = ..., zen_convert: ZenConvert | None = ...) -> type[BuildsWithSig[type[R], P]]
+ src/hydra_zen/structured_configs/_implementations.py:1130: note:     def [P`3939, R] builds(cls, Callable[P, R], /, *, zen_partial: Literal[False] | None = ..., populate_full_signature: Literal[True], zen_wrappers: Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None | Sequence[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]] | ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]] | Just[Callable[[Callable[..., Any]], Callable[..., Any]]] | type[Builds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[ZenPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[HydraPartialBuilds[Callable[[Callable[..., Any]], Callable[..., Any]]]] | type[Just[Callable[[Callable[..., Any]], Callable[..., Any]]]] | Callable[[Callable[..., Any]], Callable[..., Any]] | str | None] = ..., zen_meta: Mapping[str, SupportedPrimitive] | None = ..., hydra_recursive: bool | None = ..., hydra_convert: Literal['none', 'partial', 'all', 'object'] | None = ..., hydra_defaults: list[str | DataClass_ | type[DataClass_] | Mapping[str, str | Sequence[str] | None]] | None = ..., dataclass_name: str | None = ..., builds_bases: tuple[()] = ..., zen_dataclass: DataclassOptions | None = ..., frozen: bool = ..., zen_convert: ZenConvert | None = ...) -> type[BuildsWithSig[type[R], P]]

... (truncated 2 lines) ...

core (https://github.com/home-assistant/core)
+ homeassistant/util/frozen_dataclass_compat.py:114: error: Argument 1 to "__new__" of "object" has incompatible type "type[DataclassInstance] | DataclassInstance"; expected "type[DataclassInstance]"  [arg-type]

steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/commands.py:331: error: Incompatible types in assignment (expression has type "InvokeT", variable has type "None")  [assignment]
- steam/ext/commands/commands.py:339: error: Incompatible types in assignment (expression has type "InvokeT", variable has type "None")  [assignment]
+ steam/ext/commands/commands.py:324: error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", expected "ErrT")  [return-value]
+ steam/ext/commands/commands.py:331: error: Incompatible types in assignment (expression has type "Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", variable has type "None")  [assignment]
+ steam/ext/commands/commands.py:332: error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", expected "InvokeT")  [return-value]
+ steam/ext/commands/commands.py:339: error: Incompatible types in assignment (expression has type "Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", variable has type "None")  [assignment]
+ steam/ext/commands/commands.py:340: error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", expected "InvokeT")  [return-value]
- steam/ext/commands/help.py:138: note:          ErrT
+ steam/ext/commands/help.py:138: note:          Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]
+ steam/ext/commands/cog.py:180: error: Incompatible return value type (got "Callable[[Any, VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, None]] | Callable[[Any, VarArg(Any), KwArg(Any)], CoroutineType[Any, Any, Any]]", expected "F")  [return-value]
- steam/ext/commands/bot.py:467: error: Incompatible types in assignment (expression has type "BotInvokeT", variable has type "None")  [assignment]
- steam/ext/commands/bot.py:475: error: Incompatible types in assignment (expression has type "BotInvokeT", variable has type "None")  [assignment]
+ steam/ext/commands/bot.py:467: error: Incompatible types in assignment (expression has type "Callable[[Context[Any]], CoroutineType[Any, Any, None]] | Callable[[Context[Any]], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]", variable has type "None")  [assignment]
+ steam/ext/commands/bot.py:468: error: Incompatible return value type (got "Callable[[Context[Any]], CoroutineType[Any, Any, None]] | Callable[[Context[Any]], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]", expected "BotInvokeT")  [return-value]
+ steam/ext/commands/bot.py:475: error: Incompatible types in assignment (expression has type "Callable[[Context[Any]], CoroutineType[Any, Any, None]] | Callable[[Context[Any]], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]", variable has type "None")  [assignment]
+ steam/ext/commands/bot.py:476: error: Incompatible return value type (got "Callable[[Context[Any]], CoroutineType[Any, Any, None]] | Callable[[Context[Any]], CoroutineType[Any, Any, Any]] | Callable[..., CoroutineType[Any, Any, Any]]", expected "BotInvokeT")  [return-value]

xarray-dataclasses (https://github.com/astropenguin/xarray-dataclasses)
- xarray_dataclasses/typing.py:300: error: Unused "type: ignore" comment  [unused-ignore]

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.

Overloads not resolved correctly when argument is Any and return types use TypeGuard or TypeIs
1 participant