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

Add Python 3.11 dataclass_transform typehints #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.11"
- "3.10"
- 3.9
- 3.8
Expand Down
18 changes: 16 additions & 2 deletions zninit/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
from copy import deepcopy
from inspect import Parameter, Signature

try:
from typing import dataclass_transform # pylint: disable=no-name-in-module
except ImportError:

def dataclass_transform():
"""Empty decorator for Python < 3.11 support"""

def decorator(func):
return func

return decorator


from zninit.descriptor import Descriptor, Empty, get_descriptors

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,9 +101,9 @@ def auto_init(self, *args, **kwargs):
raise get_args_type_error(args, cls_name, uses_auto_init)
log.debug(f"The '__init__' uses auto_init: {uses_auto_init}")
for kwarg_name in kwargs_no_default:
try: # pylint: disable=loop-try-except-usage
if kwarg_name in kwargs:
init_kwargs[kwarg_name] = kwargs.pop(kwarg_name)
except KeyError:
else:
required_keys.append(kwarg_name)

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


@dataclass_transform()
class ZnInit:
"""Parent class for automatic __init__ generation based on descriptrs

Expand Down