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

Make more type expressions valid in PEP 695 aliases and runtime contexts #17404

Merged
merged 14 commits into from
Jun 20, 2024

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 18, 2024

Previously some type expressions, when used as the value of a PEP 695 type alias or in an expression context, generated errors, even if the code would work at runtime. Improve type inference of types in expression contexts (this includes PEP 695 type aliases) to better reflect runtime behavior.

This is still not perfect, since we don't have precise types for everything in stubs. Use typing._SpecialForm as a fallback, as it supports indexing and | operations, which are supported for types.

Also update stubs used in tests to better match typeshed stubs. In particular, provide _SpecialForm and define Any = object(), similar to typeshed.

Copy link
Contributor

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

steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/converters.py:510: error: Incompatible types in assignment (expression has type "object", variable has type "type[T]")  [assignment]
+ steam/ext/commands/converters.py:510: error: Incompatible types in assignment (expression has type "<typing special form>", variable has type "type[T]")  [assignment]

mypy (https://github.com/python/mypy)
+ mypy/stubgenc.py:736: error: Non-overlapping identity check (left operand type: "type", right operand type: "<typing special form>")  [comparison-overlap]
+ mypy/stubgenc.py:736: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-comparison-overlap for more info

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/annotations.py:1601: error: Incompatible return value type (got "object", expected "type[_T]")  [return-value]
+ tanjun/annotations.py:1601: error: Incompatible return value type (got "<typing special form>", expected "type[_T]")  [return-value]
- tanjun/annotations.py:1853: error: Incompatible return value type (got "object", expected "type[str]")  [return-value]
+ tanjun/annotations.py:1853: error: Incompatible return value type (got "<typing special form>", expected "type[str]")  [return-value]
- tanjun/annotations.py:2211: error: Argument 1 to "_annotated" has incompatible type "object"; expected "type[Never]"  [arg-type]
+ tanjun/annotations.py:2211: error: Argument 1 to "_annotated" has incompatible type "<typing special form>"; expected "type[Never]"  [arg-type]
- tanjun/annotations.py:2273: error: Incompatible return value type (got "object", expected "type[PartialChannel]")  [return-value]
+ tanjun/annotations.py:2273: error: Incompatible return value type (got "<typing special form>", expected "type[PartialChannel]")  [return-value]

pydantic (https://github.com/samuelcolvin/pydantic)
- pydantic/dataclasses.py:228: error: Incompatible types in assignment (expression has type "tuple[type[StandardDataclass], object]", variable has type "tuple[type[StandardDataclass]]")  [assignment]
+ pydantic/dataclasses.py:228: error: Incompatible types in assignment (expression has type "tuple[type[StandardDataclass], <typing special form>]", variable has type "tuple[type[StandardDataclass]]")  [assignment]

discord.py (https://github.com/Rapptz/discord.py)
- discord/utils.py:1092: error: "object" has no attribute "__args__"  [attr-defined]
+ discord/utils.py:1092: error: "<typing special form>" has no attribute "__args__"  [attr-defined]

ibis (https://github.com/ibis-project/ibis)
+ ibis/expr/operations/__init__.py:13: error: Incompatible import of "Any" (imported name has type "type[ibis.expr.operations.reductions.Any]", local name has type "<typing special form>")  [assignment]
- ibis/expr/types/logical.py:351: error: "object" not callable  [operator]
+ ibis/expr/types/logical.py:351: error: "<typing special form>" not callable  [operator]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:861: error: The type "type[list[Any]]" is not generic and not indexable  [misc]

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jun 19, 2024

All the mypy_primer changes seem fine. Summary of what's changed:

  • The object typing.Any is no longer considered a type. Since Any doesn't support isinstance checks, unlike regular type objects, this seems reasonable.
  • Various messages have <typing special form> instead of object, which is more informative.
  • One false positive is fixed in hydra-zen.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jun 19, 2024

I'm waiting for a review until tomorrow.

@JukkaL JukkaL merged commit c4470f1 into master Jun 20, 2024
19 checks passed
@JukkaL JukkaL deleted the type-runtime-context branch June 20, 2024 17:02
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.

None yet

2 participants