Closed not-my-profile closed 1 year ago
There's nothing in the type system that would indicate that this runtime error would occur, so there's not much pyright could do about detecting this without hard-coding some knowledge about internal workings of the associated modules and classes. We generally don't do that.
The goal of pyright isn't to catch all bugs in your code. It's a static type checker, and it's meant to help you create more maintainable and robust code.
But pyright already has special detection for this:
import typing
class Xyz:
pass
class Foo(typing.TypedDict, Xyz): ... # error: All base classes for TypedDict classes must also be TypedDict classes
My point is that the current detection isn't working correctly.
I agree that the second check I had in mind is out of scope for a static type checker and better implemented by a linter, so I just opened a respective issue at the ruff linter: charliermarsh/ruff/issues/1422
If you ban typing.TypedDict
via a lint, then the reported TypeError cannot occur anymore anyway, so I think you made the right call by closing this issue :)
The last line of the following code results in the runtime TypeError:
I just ran into this runtime error because I always default to using types from
typing
but sometimes use types fromtyping_extensions
to support older versions of Python. In particulartyping.TypedDict
is incompatible withtyping.Generic
in Python < 3.11, see https://github.com/python/cpython/pull/27663#issuecomment-1187917727, so I sometimes usedtyping_extensions.TypedDict
instead oftyping.TypedDict
.It would be nice if pyright could detect this type error, which can be quite sneaky.
I think there could even be an additional optional check that warns about this issue whenever a project uses both
typing.TypedDict
andtyping_extensions.TypedDict
in the same code base (because otherwise the public API might haveTypedDict
classes that are incompatible with each other).