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

Support both v1 and v2 of Pydantic #164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
plugins = pydantic.mypy
plugins = pydantic.v1.mypy

python_version = 3.8

Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
annotated-types==0.5.0
asn1crypto==1.4.0
black==21.9b0
cbor2==5.4.2.post1
Expand All @@ -11,11 +12,12 @@ pathspec==0.9.0
platformdirs==2.4.0
pycodestyle==2.8.0
pycparser==2.20
pydantic==1.10.11
pydantic==2.1.1
pydantic_core==2.4.0
pyflakes==2.4.0
pyOpenSSL==23.2.0
regex==2021.10.8
six==1.16.0
toml==0.10.2
tomli==1.2.1
typing-extensions==4.2.0
typing_extensions==4.7.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def find_version(*file_paths):
'asn1crypto>=1.4.0',
'cbor2>=5.4.2.post1',
'cryptography>=41.0.1',
'pydantic>=1.10.11,<2.0a0',
'pydantic>=1.10.11',
'pyOpenSSL>=23.2.0',
]
)
5 changes: 4 additions & 1 deletion webauthn/helpers/decode_credential_public_key.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Union

from cbor2 import decoder
from pydantic import BaseModel
try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the #type: ignore's on the v2 imports because mypy, referencing v1 types, would complain about these?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports in the except block are actually "v1" imports.

If we can't import from pydantic.v1 (which only exists in v2), then we instead try to import from pydantic (assuming that v1 is installed).

Because in the repo itself we have v2 installed, I've put the ignores on the "v1" imports because when v2 is installed that's actually importing the new v2 API which conflicts with the old v1 version.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right pydantic.v1 is the new import 🤦‍♂️

So would this mean users still using Pydantic v1 in their project wouldn't get mypy-assisted type annotations on the methods in this library if they're still on Pydantic v1? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, I think that's probably right (I have no idea how you'd go about supporting that either) 😬

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, depending on how much of the internal pydantic stuff is being exposed, I suspect it probably won't be that much of a degraded experience for those folks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would this mean users still using Pydantic v1 in their project wouldn't get mypy-assisted type annotations on the methods in this library if they're still on Pydantic v1? 🤔

From what I've seen mypy will also complain about a lot of stuff if you have v2 installed and use the v1 imports, at least if the mypy plugin for pydantic is enabled.


from .cose import COSECRV, COSEKTY, COSEAlgorithmIdentifier, COSEKey
from .exceptions import InvalidPublicKeyStructure, UnsupportedPublicKeyType
Expand Down
5 changes: 4 additions & 1 deletion webauthn/helpers/parse_backup_flags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from enum import Enum
from pydantic import BaseModel
try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel # type: ignore

from .structs import AuthenticatorDataFlags, CredentialDeviceType
from .exceptions import InvalidBackupFlags
Expand Down
5 changes: 4 additions & 1 deletion webauthn/helpers/parse_client_data_json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
from json.decoder import JSONDecodeError

from pydantic import ValidationError
try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError # type: ignore

from .base64url_to_bytes import base64url_to_bytes
from .exceptions import InvalidClientDataJSONStructure
Expand Down
8 changes: 6 additions & 2 deletions webauthn/helpers/structs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from enum import Enum
from typing import List, Literal, Optional

from pydantic import BaseModel, validator
from pydantic.fields import ModelField
try:
from pydantic.v1 import BaseModel, validator
from pydantic.v1.fields import ModelField
except:
from pydantic import BaseModel, validator # type: ignore
from pydantic.fields import ModelField # type: ignore

from .bytes_to_base64url import bytes_to_base64url
from .cose import COSEAlgorithmIdentifier
Expand Down