Skip to content

Commit

Permalink
Merge pull request #75 from linkml/issue_73
Browse files Browse the repository at this point in the history
Issue 73
  • Loading branch information
hsolbrig authored Nov 23, 2021
2 parents 75cf495 + 7db6146 commit 3759ed3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/pr-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ jobs:

test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.7.1, 3.8, 3.9 ]
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: ${{ matrix.python-version }}
- name: Install pipenv
uses: dschep/install-pipenv-action@v1
- name: Install dependencies and test
run: |
pipenv install --dev
pipenv install --dev --python ${{ matrix.python-version }}
pipenv run python -m unittest
61 changes: 61 additions & 0 deletions linkml_runtime/utils/dataclass_extensions_376.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,67 @@

if sys.version_info < (3, 7, 0):
raise NotImplementedError("LinkML requires Python 3.7 or later to run")
elif sys.version_info >= (3, 10, 0):
from dataclasses import MISSING, _HAS_DEFAULT_FACTORY, _POST_INIT_NAME, _FIELD_INITVAR, _init_param, _field_init, \
_create_fn

def dataclasses_init_fn_with_kwargs(fields, std_fields, kw_only_fields, frozen, has_post_init,
self_name, globals):
# fields contains both real fields and InitVar pseudo-fields.

# Make sure we don't have fields without defaults following fields
# with defaults. This actually would be caught when exec-ing the
# function source code, but catching it here gives a better error
# message, and future-proofs us in case we build up the function
# using ast.

seen_default = False
for f in std_fields:
# Only consider the non-kw-only fields in the __init__ call.
if f.init:
if not (f.default is MISSING and f.default_factory is MISSING):
seen_default = True
elif seen_default:
raise TypeError(f'non-default argument {f.name!r} '
'follows default argument')

locals = {f'_type_{f.name}': f.type for f in fields}
locals.update({
'MISSING': MISSING,
'_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY,
})

body_lines = []
for f in fields:
line = _field_init(f, frozen, locals, self_name)
# line is None means that this field doesn't require
# initialization (it's a pseudo-field). Just skip it.
if line:
body_lines.append(line)

# Does this class have a post-init function?
if has_post_init:
params_str = ','.join(f.name for f in fields
if f._field_type is _FIELD_INITVAR)
body_lines.append(f'{self_name}.{_POST_INIT_NAME}({params_str}{", " if params_str else ""} **kwargs)')

# If no body lines, use 'pass'.
if not body_lines:
body_lines = ['pass']

_init_params = [_init_param(f) for f in std_fields]
if kw_only_fields:
# Add the keyword-only args. Because the * can only be added if
# there's at least one keyword-only arg, there needs to be a test here
# (instead of just concatenting the lists together).
_init_params += ['*']
_init_params += [_init_param(f) for f in kw_only_fields]
return _create_fn('__init__',
[self_name] + _init_params + ["**kwargs"],
body_lines,
locals=locals,
globals=globals,
return_type=None)
elif sys.version_info >= (3, 7, 6):
from dataclasses import MISSING, _HAS_DEFAULT_FACTORY, _POST_INIT_NAME, _FIELD_INITVAR, _init_param, _field_init, _create_fn

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py37, py38, py39
envlist = py37, py38, py39, py310


[testenv]
Expand Down

0 comments on commit 3759ed3

Please sign in to comment.