From 7821a7945bc80cf5b79cc1afe548df5c49dba9c6 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Tue, 10 Oct 2023 10:11:50 +0200 Subject: [PATCH] Support and test internal fields on Python up to 3.12 --- .github/workflows/pythonapp.yml | 2 +- pylasu/model/model.py | 9 +++++++-- tests/fixtures.py | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index 6692b26..a7fc8a6 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -6,7 +6,7 @@ jobs: build: strategy: matrix: - python: [ '3.7', '3.8', '3.9', '3.10', '3.11' ] + python: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12' ] runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/pylasu/model/model.py b/pylasu/model/model.py index d0452d6..65bd17d 100644 --- a/pylasu/model/model.py +++ b/pylasu/model/model.py @@ -25,12 +25,17 @@ class InternalField(Field): def internal_field( - *, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None): + *, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, + kw_only=False): """Return an object to identify internal dataclass fields. The arguments are the same as dataclasses.field.""" if default is not MISSING and default_factory is not MISSING: raise ValueError('cannot specify both default and default_factory') - return InternalField(default, default_factory, init, repr, hash, compare, metadata) + try: + # Python 3.10+ + return InternalField(default, default_factory, init, repr, hash, compare, metadata, kw_only) + except: + return InternalField(default, default_factory, init, repr, hash, compare, metadata) class Origin(ABC): diff --git a/tests/fixtures.py b/tests/fixtures.py index 91f250c..79a0a8c 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,13 +1,14 @@ from dataclasses import dataclass, field from typing import List -from pylasu.model import Node, pos +from pylasu.model import Node, pos, internal_field @dataclass class Box(Node): name: str = None contents: List[Node] = field(default_factory=list) + internal: str = internal_field(default="unused") @dataclass