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

Using TypeAdapter with PyRight has typing issues #9147

Open
2 of 13 tasks
skewty opened this issue Apr 2, 2024 · 7 comments
Open
2 of 13 tasks

Using TypeAdapter with PyRight has typing issues #9147

skewty opened this issue Apr 2, 2024 · 7 comments
Labels
feature request urls Related to `Url` types

Comments

@skewty
Copy link
Contributor

skewty commented Apr 2, 2024

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Hoping something can be done with typing to make it get along better with PyRight. See below:

from pydantic import AnyHttpUrl, TypeAdapter

def ensure_url(url: AnyHttpUrl | str) -> AnyHttpUrl:
    if isinstance(url, str):
        return TypeAdapter(AnyHttpUrl).validate_python(url)  # pyright: ignore [reportCallIssue, reportReturnType]
    return url

Affected Components

@Viicos
Copy link
Contributor

Viicos commented Apr 2, 2024

Duplicate of #7094

@sydney-runkle
Copy link
Member

@Viicos,

Any suggestions for how we might fix this?

@skewty
Copy link
Contributor Author

skewty commented Apr 2, 2024

Additionally AnyHttpUrl (and I am expecting the other variants too) cannot be used with isinstance() which is counter intuitive.
Perhaps proper classes should be created and used? class Url(str) and all the various subclasses with different validation requirements.

@Viicos
Copy link
Contributor

Viicos commented Apr 3, 2024

Any suggestions for how we might fix this?

#8923 will suppress the error, however the type variable of TypeAdapter will be inferred as TypeAdapter[Any].

To properly support this I'm afraid the only solution is to have type forms to land, but this is an active subject: python/mypy#9773.

Additionally AnyHttpUrl (and I am expecting the other variants too) cannot be used with isinstance() which is counter intuitive.

I agree this can be counter intuitive. Switching from Annotated constraints to proper subclasses probably requires a lot of work as well :/

@sydney-runkle
Copy link
Member

Yeah, I think we do actually want to switch to proper subclasses: #7353. So I think that's probably the way forward here.

@sydney-runkle
Copy link
Member

For now, let's have the subclasses replace the exiting types in pydantic, then we can move them to pydantic-extra-types when we do a V3 release.

@skewty
Copy link
Contributor Author

skewty commented Apr 3, 2024

For now, let's have the subclasses replace the exiting types in pydantic, then we can move them to pydantic-extra-types when we do a V3 release.

Great time to address this issue #9159.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request urls Related to `Url` types
Projects
None yet
Development

No branches or pull requests

3 participants