Skip to content

Add support for type aliases using the "type" keyword#287

Open
Kyoya2 wants to merge 2 commits intokonradhalas:masterfrom
Kyoya2:master
Open

Add support for type aliases using the "type" keyword#287
Kyoya2 wants to merge 2 commits intokonradhalas:masterfrom
Kyoya2:master

Conversation

@Kyoya2
Copy link
Copy Markdown

@Kyoya2 Kyoya2 commented Oct 18, 2025

Starting from Python 3.12, it's possible to define a type alias using the following syntax:

type MyAlias = str

This creates a typing.TypeAliasType object under the name MyAlias.

I added support for such type aliases, along with some tests.

I should note that some of the new tests (like test_is_type_alias) should've been added to tests/test_types.py. Instead, I placed them in tests/core/test_type_alias.py, because using the "type" keyword on Python versions older than 3.12 would raise a SyntaxError when the file is evaluated. Which is why the entire file tests/core/test_type_alias.py is excluded from testing on those older versions.

Comment thread dacite/types.py
Comment on lines +147 to +154
if version_info >= (3, 14):
@cache
def extract_type_alias(type_: TypeAliasType) -> Type:
return type_.evaluate_value()
else:
@cache
def extract_type_alias(type_: TypeAliasType) -> Type:
return type_.__value__
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This if statement is technically unnecessary, since the else branch will always work. However, I believe that it's best practice to avoid accessing "internal" attributes such as __value__ when possible. I can get rid of it if desired.

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

Successfully merging this pull request may close these issues.

1 participant