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

Work around mypy limitations #191

Merged
merged 1 commit into from
Sep 19, 2022
Merged

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Sep 19, 2022

Currently mypy doesn't interpret things other than classes as being compatible with Type[T] (they literally have to be instances of type). This makes it hard to properly type the decode methods or Decoder constructors, as passing type-like-things (e.g. Union[int, str]) will error under mypy. pyright doesn't have this limitation.

For now we add a fallback overload to these methods that infers the decoder type as Decoder[Any]. This means that mypy will no longer error, but will require an explicit annotation to infer the type of the output of decode.

If/once the TypeForm PEP lands, this hack can be removed. Until then pyright support for these annotations will be much better than mypy support.

Fixes #173.

Currently mypy doesn't interpret things other than classes as being
compatible with `Type[T]` (they literally have to be instances of
`type`). This makes it hard to properly type the `decode` methods or
`Decoder` constructors, as passing type-like-things (e.g. `Union[int,
str]`) will error under mypy. `pyright` doesn't have this limitation.

For now we add a fallback `overload` to these methods that infers the
decoder type as `Decoder[Any]`. This means that `mypy` will no longer
error, but will require an explicit annotation to infer the type of the
output of `decode`.

If/once the `TypeForm` PEP lands, this hack can be removed. Until then
`pyright` support for these annotations will be much better than `mypy`
support.
@jcrist jcrist merged commit 1605eb7 into main Sep 19, 2022
@jcrist jcrist deleted the work-around-mypy-type-limitation branch September 19, 2022 15:42
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.

mypy error with Tagged Unions
1 participant