-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Adding a classmethod to TypedDict with "# type: ignore" crashes mypy #9820
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
Comments
Rewriting the type definitions like this: class HardwareMetadata(TypedDict):
status: Literal["healthy", "degraded", "offline", "unavailable"]
status_info: Optional[str]
metadata: Mapping[str, str]
def check(cls, value: Any) -> HardwareMetadata:
try:
return t.Dict({
t.Key('status'): t.Enum("healthy", "degraded", "offline", "unavailable"),
t.Key('status_info'): t.Null | t.String,
t.Key('metadata'): t.Mapping(t.String, t.String),
}).check(value)
except t.DataError as e:
raise ValueError("The give value does not conform with the target typed dict.", e.as_dict())
HardwareMetadata.check = classmethod(check) # type: ignore "fixes" the crash, but I need to put |
It seems that the current "correct" workaround is to just separate the check method as an independent function. |
For instance, maybe we could just extend mypy to recognize trafaret validators as static type definitions of corresponding Python types. Though, I don't know the details about mypy extensions, so I'm not sure that this would be a good approach for this issue. |
I ended up combining the original TypedDict definition and |
u can try about from typing import Self
class MyClass:
def method(self) -> Self:
return self |
Uh oh!
There was an error while loading. Please reload this page.
Crash Report / To Reproduce
I know that currently allowing adding class methods to TypedDict is not on the priority (#5653),
so I tried to use it anyway with
# type: ignore
.I'm referring this as
HardwareMetadata.check(...)
in another module.My intention is to validate JSON-like semi-structured data at runtime and use it safely when passing around the codebase with static type check.
Then, running mypy began to crash.
I think it should not crash anyway even though it does not allow this pattern.
Traceback
Your Environment
python -m mypy src/ai/backend tests --show-traceback
ai.backend
is the namespace pkg.mypy.ini
(and other config files): I have the following insetup.cfg
:The text was updated successfully, but these errors were encountered: