You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RootModel classes should be able to take one argument of the root model type and initialize it that way. From the docs: "The root value can be passed to the model init or model_validate via the first and only argument."
Attempting to initialize a FancyID in the example with a simple string reports "expected 0 positional arguments".
Note that the code works in all cases, however the type system (Pylance in this case) does not understand it correctly. Moreover, workarounds like the one in HackyID below, or only initializing FancyID with model_validate() also work.
In the example SimpleID and HackyID work and report nothing wrong, and FancyID also works but reports "Expected 0 positional arguments Pylance(reportGeneralTypeIssues)".
Example Code
frompydanticimportRootModelSimpleID=RootModel[str]
classFancyID(RootModel[str]):
def__str__(self) ->str:
returnself.rootclassHackyID(RootModel[str]):
def__init__(self, root: str):
super().__init__(root=root)
def__str__(self) ->str:
returnself.root# from https://docs.pydantic.dev/2.5/concepts/models/#rootmodel-and-custom-root-types# "The root value can be passed to the model __init__ or model_validate via the first and only argument."foo=SimpleID('foo')
bar=FancyID('bar') # Expected 0 positional arguments Pylance(reportGeneralTypeIssues)hack=HackyID('hack')
print(foo)
print(bar)
print(hack)
assertstr(bar) =='bar'# works just fineassertstr(hack) =='hack'bar=FancyID.model_validate('bar') # no type errors
Thanks for reporting this. Looks like we fixed this issue for the mypy case here: #7677.
@Viicos, any idea what we might be able to do here for a fix? I know you did some work recently with the dataclass_transform logic that might be relevant. Not sure if this is easily fixable, though.
@WarpedPixel you are still on 2.5.x, where a positional argument to a RootModel was not allowed. #7677 fixed for mypy but for pyright as well. This is a MRE for pyright, showing it is working fine.
Initial Checks
Description
RootModel
classes should be able to take one argument of the root model type and initialize it that way. From the docs: "The root value can be passed to the model init or model_validate via the first and only argument."Attempting to initialize a
FancyID
in the example with a simple string reports "expected 0 positional arguments".Note that the code works in all cases, however the type system (Pylance in this case) does not understand it correctly. Moreover, workarounds like the one in
HackyID
below, or only initializingFancyID
withmodel_validate()
also work.In the example SimpleID and HackyID work and report nothing wrong, and FancyID also works but reports "Expected 0 positional arguments Pylance(reportGeneralTypeIssues)".
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: