Skip to content

Work around mypy limitations - #191

Merged
jcrist merged 1 commit into
mainfrom
work-around-mypy-type-limitation
Sep 19, 2022
Merged

jcrist merged 1 commit into
mainfrom
work-around-mypy-type-limitation

Conversation

@jcrist

@jcrist jcrist commented Sep 19, 2022

Copy link
Copy Markdown
Member

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