-
Notifications
You must be signed in to change notification settings - Fork 9
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
Partial classes cannot be used in type annotations #2
Comments
I initially thought that adding a real class will fix this, but mypy cannot resolve the type then: from pydantic import BaseModel
from pydantic_partial import PartialModelMixin
class Foo(PartialModelMixin, BaseModel):
id: int
class PartialFoo(Foo.as_partial()):
pass
reveal_type(Foo())
reveal_type(PartialFoo())
def something(x: PartialFoo):
return x.dict() ...this will still give you The main issue is, that we are constructing a type during runtime. This dynamic type is not supported by mypy at all. I tried to mark You can work around this issue by tricking mypy into not seeing the conversion to partial at all. Note however that this still means all partial model instances will just be seen as instanced of See the following example code: from typing import TYPE_CHECKING
from pydantic import BaseModel
from pydantic_partial import PartialModelMixin
class Foo(PartialModelMixin, BaseModel):
id: int
if TYPE_CHECKING:
PartialFoo = Foo
else:
PartialFoo = Foo.as_partial()
reveal_type(Foo())
reveal_type(PartialFoo())
def something(x: PartialFoo):
return x.dict() Which will produce the following mypy output:
|
Side note: If anyone knows a better way to solve this or how to define the types in this library, please feel free to send me some suggestions or even a pull request. ;-) |
Currently my feeling is I have to write a mypy plugin... 🤷♂️🤔 |
The ideal solution in my mind would look something like PartialFoo = Partial[Foo] if only |
@jdinunzio Yeah, that would be the best syntax. Sadly using If anyone else is interested in solving this a PR would be very much appreciated. 👍 |
About |
Note: pydantic itself thought about adding partial support, but then decided to not do this for now. Reason is - like with this ticket - that there is no good way to get the typing definition done, as there is no partial equivalent in the python typing system now. As of this I will do the same and kind of ignore the fact that pydantic-partial will not (and kind of cannot) produce partial models in a way type checkers could recognise. There is just no base typing mechanism to support this. See pydantic/pydantic#1673 (comment) for reference. Note: I will keep this issue open to have this documented. It still is an open issue - but just one we cannot resolve in a good way. |
Issue
Using python 3.10.7, pydantic 4.3.2, pydantic-partial 0.3.2 and 0.3.3, mypy 0.971
running mypy will return:
(revealed type for
PartialFoo()
is "Any" in 0.3.2 and "foo.Foo" in 0.3.3).Question / Request
How to use partials as type annotations? If there's a way to do it, could it be documented?
The text was updated successfully, but these errors were encountered: