Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 5627b08

Browse files
committedOct 25, 2022
Add Python 3.11 dataclass_transform typehints
1 parent 31fd8bc commit 5627b08

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
 

‎.github/workflows/pytest.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
python-version:
20+
- "3.11"
2021
- "3.10"
2122
- 3.9
2223
- 3.8

‎zninit/core/__init__.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
from copy import deepcopy
77
from inspect import Parameter, Signature
88

9+
try:
10+
from typing import dataclass_transform # pylint: disable=no-name-in-module
11+
except ImportError:
12+
13+
def dataclass_transform():
14+
"""Empty decorator for Python < 3.11 support"""
15+
16+
def decorator(func):
17+
return func
18+
19+
return decorator
20+
21+
922
from zninit.descriptor import Descriptor, Empty, get_descriptors
1023

1124
log = logging.getLogger(__name__)
@@ -88,9 +101,9 @@ def auto_init(self, *args, **kwargs):
88101
raise get_args_type_error(args, cls_name, uses_auto_init)
89102
log.debug(f"The '__init__' uses auto_init: {uses_auto_init}")
90103
for kwarg_name in kwargs_no_default:
91-
try: # pylint: disable=loop-try-except-usage
104+
if kwarg_name in kwargs:
92105
init_kwargs[kwarg_name] = kwargs.pop(kwarg_name)
93-
except KeyError:
106+
else:
94107
required_keys.append(kwarg_name)
95108

96109
if len(required_keys) > 0:
@@ -116,6 +129,7 @@ def auto_init(self, *args, **kwargs):
116129
return auto_init
117130

118131

132+
@dataclass_transform()
119133
class ZnInit:
120134
"""Parent class for automatic __init__ generation based on descriptrs
121135

0 commit comments

Comments
 (0)
This repository has been archived.