-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lock on latest jsonasobj for integration
- Loading branch information
Showing
6 changed files
with
149 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,107 @@ | ||
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, 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 fields: | ||
# Only consider 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'] | ||
|
||
return _create_fn('__init__', | ||
[self_name] + [_init_param(f) for f in fields if f.init] + ["**kwargs"], | ||
body_lines, | ||
locals=locals, | ||
globals=globals, | ||
return_type=None) | ||
import sys | ||
|
||
if sys.version_info < (3, 7, 0): | ||
raise NotImplementedError("LinkML requires Python 3.7 or later to run") | ||
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 | ||
|
||
|
||
def dataclasses_init_fn_with_kwargs(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 fields: | ||
# Only consider 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'] | ||
|
||
return _create_fn('__init__', | ||
[self_name] + [_init_param(f) for f in fields if f.init] + ["**kwargs"], | ||
body_lines, | ||
locals=locals, | ||
globals=globals, | ||
return_type=None) | ||
else: | ||
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, frozen, has_post_init, self_name): | ||
# 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 fields: | ||
# Only consider 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') | ||
|
||
globals = {'MISSING': MISSING, | ||
'_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY} | ||
|
||
body_lines = [] | ||
for f in fields: | ||
line = _field_init(f, frozen, globals, 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'] | ||
|
||
locals = {f'_type_{f.name}': f.type for f in fields} | ||
return _create_fn('__init__', | ||
[self_name] + [_init_param(f) for f in fields if f.init] + ["**kwargs"], | ||
body_lines, | ||
locals=locals, | ||
globals=globals, | ||
return_type=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters