-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Pydantic Field alias - wrong invalid string reporting #9386
Comments
This change was intentional. See this issue. How would you construct an instance of the Part of the challenge here is that the Right now, the typing spec says " @Viicos, do you have any thoughts on this? |
In Pydantic, you can also construct a model using the class Resource(BaseModel):
odataType: str = Field(alias="@odata.type")
resource = Resource.model_validate({'@odata.type': '...'}) Where it makes sense to use invalid Python identifiers as keys. Here are a couple options that could be considered:
from typing import TypedDict, Unpack
TD = TypedDict('TD', {'@odata.type': str})
def func(**kwargs: Unpack[TD]): ...
func(**{'@odata.type': '...'})
@yann-combarnous: alternatively, you can set the from typing import Annotated
class Resource(BaseModel):
odataType: Annotated[str, Field(alias="@odata.type")] = "a"
# Note that pyright won't be able to understand that the following field
# has a default value:
test: Annotated[str, Field(default="a")]
Resource() # pyright error, missing value for argument `test` |
Oh, that's a good point about using an unpacked dictionary (as in @debonte, based on this feedback, I think I will undo the change I made for #9220. This isn't an error condition unless/until you attempt to use the illegal identifier as as keyword argument. Pyright already flags that as an error. The error check I added for #9220 is therefore a false positive. If I revert the change for #9220, the completion provider logic will then need to become smarter to avoid recommending an illegal keyword argument identifier. Here's a similar case — involving a TypedDict rather than a dataclass_transform class — where the completion provider produces problematic completion suggestions: TD1 = TypedDict("TD1", {"@illegal param": int})
td1 = TD1(**{"@illegal param": 3}) |
Sounds good, thanks. |
This is addressed in pyright 1.1.388. |
Describe the bug
For Pydantic field alias, valid strings are reported as being invalid.
Code or Screenshots
alias is reported as
str | None
, but pyright raisesreportGeneralTypeIssues
pydantic = "2.9.2"
VS Code extension or command-line
Using VSCode Pylance, also reported using jakebailey/pyright-action, using pylance-version: latest.
This is a new issue, was not reported prior to last week.
The text was updated successfully, but these errors were encountered: