Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.2k
Fix type checking issue withmodel_fields
andmodel_computed_fields
#10911
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
cloudflare-workers-and-pagesbot commentedNov 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Deploying pydantic-docs with |
Latest commit: | 60522af |
Status: | ✅ Deploy successful! |
Preview URL: | https://88b7606a.pydantic-docs.pages.dev |
Branch Preview URL: | https://fix-type-checking-issue-mode.pydantic-docs.pages.dev |
codspeed-hqbot commentedNov 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
CodSpeed Performance ReportMerging#10911 willnot alter performanceComparing Summary
|
github-actionsbot commentedNov 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Coverage reportClick to see where and how coverage changed
This report was generated bypython-coverage-comment-action |
Viicos commentedNov 21, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
We are hitting undefined behavior between pyright and mypy here, when a property is defined both on the metaclass and the class itself. I think it's a bit unfortunate to have
Code sample inpyright playground importwarningsfromtypingimportCallable,Generic,TypeVar,overloadfromtyping_extensionsimportdeprecatedfrompydantic.warningsimportPydanticDeprecatedSince211ModelT=TypeVar('ModelT',bound='BaseModel')R=TypeVar('R')classclassonlyproperty(Generic[ModelT,R]):def__init__(self,fget:Callable[[type[ModelT]],R],/)->None:self.fget=fget@overloaddef__get__(self,instance:None,owner:type[ModelT])->R: ...@overload@deprecated("Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3.",category=None, )def__get__(self,instance:ModelT,owner:type[ModelT])->R: ...def__get__(self,instance:ModelT|None,owner:type[ModelT])->R:ifinstanceisnotNone:warnings.warn("Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3.",category=PydanticDeprecatedSince211, )returnself.fget(owner)classBaseModel:@classonlyproperty@classmethoddefmodel_fields(cls)->dict[str,str]: ...BaseModel.model_fieldsBaseModel().model_fields# type checker error pyright will emit an error, and mypy
WDYT? |
Interesting idea. 2 reservations I have about this:
I sort of poorly explained this in a comment:
|
Viicos commentedNov 21, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The naming of the decorator can be improved, but with this approach, accessing the propertydoes work at runtime. However, it will emit a runtime deprecation warning and a type checker error/warning
Well the usage of the descriptor can feel a bit too much for such a small use case, but descriptor support is implemented by all major type checkers and this seems to be the only viable approach to emit a deprecation warning. I'm a bit worried that in V3 this will come as a surprise, because we currently don't emit anything (as an example, SQLModelrelies on this) |
Yeah, fair point. What if:
|
Sounds good, could you please add some tests in the |
Oh so odd, I think I have them locally but didn't push... |
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
thanks for adding the tests
9915abf
intomainUh oh!
There was an error while loading.Please reload this page.
…s` (#10911)Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Fix#10907