Skip to content
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

Type stubs: Incorrect return type on validate_assignment #947

Open
hassec opened this issue Sep 5, 2023 · 2 comments
Open

Type stubs: Incorrect return type on validate_assignment #947

hassec opened this issue Sep 5, 2023 · 2 comments
Assignees

Comments

@hassec
Copy link

hassec commented Sep 5, 2023

Is it possible that the below type stub is incorrect?

def validate_assignment(
self,
obj: Any,
field_name: str,
field_value: Any,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: 'dict[str, Any] | None' = None,
) -> dict[str, Any] | tuple[dict[str, Any], dict[str, Any] | None, set[str]]:

In the below example the returned value seems to be a of type A.

from pydantic import BaseModel

class A(BaseModel, validate_assignment=True):
    i: int

a = A(i=1)
x = a.__pydantic_validator__.validate_assignment(a, "i", 3)
print(type(x))  # <class '__main__.A'>
print(x is a)  # True
@davidhewitt
Copy link
Contributor

Yes, the type stub looks pretty wonky. In general this should return the model / dataclass / TypedDict instance which has been validated. There is some complexity on the Rust side which is probably what led to this stub. @adriangb maybe you have an idea how we might be able to improve this?

@davidhewitt
Copy link
Contributor

Maybe it's better to just make the type stub here -> Any?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants