-
-
Notifications
You must be signed in to change notification settings - Fork 781
✨ Add support for PEP695 type alias syntax
#1628
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
Open
stickm4n
wants to merge
20
commits into
fastapi:main
Choose a base branch
from
stickm4n:type-alias-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f7d86ec
📝 Fix type alias handling in _compat.py
stickm4n a7615de
📝 Add tests for type alias handling in test_field_sa_type.py
stickm4n 105a8e5
📝 Update type alias definitions in tests and compatibility module for…
stickm4n 9d63657
📝 Update type alias handling for Python 3.12 compatibility
stickm4n 5954463
📝 Refactor tests in test_field_sa_type.py for Python 3.12 compatibility
stickm4n 69a2b21
📝 Enhance type alias handling in compatibility module and tests for P…
stickm4n 05d32da
📝 Remove Python 3.12 compatibility marker from tests in test_field_sa…
stickm4n a05a685
📝 Add handling for GenericAlias in type alias checks for Python 3.10
stickm4n fc9a2e6
📝 Refactor type alias handling and add tests for NewType and TypeVar …
stickm4n 506e5c1
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 80f7a09
📝 Fix type alias check to ensure check_type is not empty before valid…
stickm4n a2303b3
Merge remote-tracking branch 'origin/type-alias-fix' into type-alias-fix
stickm4n 8fe139b
📝 Fix type alias check to ensure check_type is not empty before valid…
stickm4n 563fa92
📝 Update type alias definitions to support conditional assignment for…
stickm4n a86418c
📝 Update tests to conditionally define type aliases based on availabi…
stickm4n daadf43
📝 Update _is_new_type_instance to handle Python 3.10 compatibility
stickm4n d128318
📝 Add needs_pydanticv2 decorator to tests for Pydantic v2 compatibility
stickm4n 1e9cf12
📝 Refactor type alias handling to improve compatibility with Python 3…
stickm4n 2cc8cd0
Update _compat.py
stickm4n 702ac38
Update _compat.py
stickm4n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import typing as t | ||
| from textwrap import dedent | ||
|
|
||
| import pytest | ||
| import typing_extensions as te | ||
| from sqlmodel import Field, SQLModel | ||
|
|
||
| from tests.conftest import needs_py312, needs_pydanticv2 | ||
|
|
||
|
|
||
| def test_sa_type_typing_1() -> None: | ||
| Type1_t = str | ||
|
|
||
| class Hero1(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type1_t = "sword" | ||
|
|
||
|
|
||
| if hasattr(t, "Annotated"): | ||
|
|
||
| def test_sa_type_typing_2() -> None: | ||
| Type2_t = t.Annotated[str, "Just a comment"] | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type2_t = "sword" | ||
|
|
||
|
|
||
| if hasattr(t, "TypeAlias"): | ||
| Type3_t: t.TypeAlias = str | ||
|
|
||
| def test_sa_type_typing_3() -> None: | ||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type3_t = "sword" | ||
|
|
||
| if hasattr(t, "Annotated"): | ||
| Type4_t: t.TypeAlias = t.Annotated[str, "Just a comment"] | ||
|
|
||
| def test_sa_type_typing_4() -> None: | ||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type4_t = "sword" | ||
|
|
||
|
|
||
| @needs_py312 | ||
| @needs_pydanticv2 | ||
| def test_sa_type_typing_5() -> None: | ||
| test_code = dedent(""" | ||
| type Type5_t = str | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type5_t = "sword" | ||
| """) | ||
| exec(test_code, globals()) | ||
|
|
||
|
|
||
| @needs_py312 | ||
| @needs_pydanticv2 | ||
| def test_sa_type_typing_6() -> None: | ||
| test_code = dedent(""" | ||
| type Type6_t = t.Annotated[str, "Just a comment"] | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type6_t = "sword" | ||
| """) | ||
| exec(test_code, globals()) | ||
|
|
||
|
|
||
| def test_sa_type_typing_7() -> None: | ||
| Type7_t = t.NewType("Type7_t", str) | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type7_t = "sword" | ||
|
|
||
|
|
||
| def test_sa_type_typing_8() -> None: | ||
| Type8_t = t.TypeVar("Type8_t", bound=str) | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type8_t = "sword" | ||
|
|
||
|
|
||
| def test_sa_type_typing_9() -> None: | ||
| Type9_t = t.TypeVar("Type9_t", str, bytes) | ||
|
|
||
| with pytest.raises(ValueError): | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type9_t = "sword" | ||
|
|
||
|
|
||
| def test_sa_type_typing_extensions_1() -> None: | ||
| Type1_te = str | ||
|
|
||
| class Hero1(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type1_te = "sword" | ||
|
|
||
|
|
||
| if hasattr(te, "Annotated"): | ||
|
|
||
| def test_sa_type_typing_extensions_2() -> None: | ||
| Type2_te = te.Annotated[str, "Just a comment"] | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type2_te = "sword" | ||
|
|
||
|
|
||
| if hasattr(te, "TypeAlias"): | ||
| Type3_te: te.TypeAlias = str | ||
|
|
||
| def test_sa_type_typing_extensions_3() -> None: | ||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type3_te = "sword" | ||
|
|
||
| if hasattr(te, "Annotated"): | ||
| Type4_te: te.TypeAlias = te.Annotated[str, "Just a comment"] | ||
|
|
||
| def test_sa_type_typing_extensions_4() -> None: | ||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type4_te = "sword" | ||
|
|
||
|
|
||
| @needs_py312 | ||
| @needs_pydanticv2 | ||
| def test_sa_type_typing_extensions_5() -> None: | ||
| test_code = dedent(""" | ||
| type Type5_te = str | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type5_te = "sword" | ||
| """) | ||
| exec(test_code, globals()) | ||
|
|
||
|
|
||
| @needs_py312 | ||
| @needs_pydanticv2 | ||
| def test_sa_type_typing_extensions_6() -> None: | ||
| test_code = dedent(""" | ||
| type Type6_te = te.Annotated[str, "Just a comment"] | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type6_te = "sword" | ||
| """) | ||
| exec(test_code, globals()) | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_sa_type_typing_extensions_7() -> None: | ||
| Type7_te = te.NewType("Type7_te", str) | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type7_te = "sword" | ||
|
|
||
|
|
||
| def test_sa_type_typing_extensions_8() -> None: | ||
| Type8_te = te.TypeVar("Type8_te", bound=str) | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type8_te = "sword" | ||
|
|
||
|
|
||
| def test_sa_type_typing_extensions_9() -> None: | ||
| Type9_te = te.TypeVar("Type9_te", str, bytes) | ||
|
|
||
| with pytest.raises(ValueError): | ||
|
|
||
| class Hero(SQLModel, table=True): | ||
| pk: int = Field(primary_key=True) | ||
| weapon: Type9_te = "sword" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.