Skip to content

Instantly share code, notes, and snippets.

@1st1
Last active September 23, 2018 16:40
Show Gist options
  • Save 1st1/37fdd3cc84cd65b9af3471b935b722df to your computer and use it in GitHub Desktop.
Save 1st1/37fdd3cc84cd65b9af3471b935b722df to your computer and use it in GitHub Desktop.

Revisions

  1. 1st1 revised this gist Sep 22, 2018. 1 changed file with 1 addition and 10 deletions.
    11 changes: 1 addition & 10 deletions fix_dataclass_introspection.diff
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,7 @@
    diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
    index 28e9f75127..57951fdfc1 100644
    index 28e9f75127..ad3dc46b67 100644
    --- a/Lib/dataclasses.py
    +++ b/Lib/dataclasses.py
    @@ -448,7 +448,7 @@ def _init_param(f):
    return f'{f.name}:_type_{f.name}{default}'


    -def _init_fn(fields, frozen, has_post_init, self_name):
    +def _init_fn(fields, frozen, has_post_init, self_name, modname=None):
    # fields contains both real fields and InitVar pseudo-fields.

    # Make sure we don't have fields without defaults following fields
    @@ -868,7 +868,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
    # Include InitVars and regular fields (so, not ClassVars).
    flds = [f for f in fields.values()
  2. 1st1 created this gist Sep 22, 2018.
    48 changes: 48 additions & 0 deletions fix_dataclass_introspection.diff
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
    index 28e9f75127..57951fdfc1 100644
    --- a/Lib/dataclasses.py
    +++ b/Lib/dataclasses.py
    @@ -448,7 +448,7 @@ def _init_param(f):
    return f'{f.name}:_type_{f.name}{default}'


    -def _init_fn(fields, frozen, has_post_init, self_name):
    +def _init_fn(fields, frozen, has_post_init, self_name, modname=None):
    # fields contains both real fields and InitVar pseudo-fields.

    # Make sure we don't have fields without defaults following fields
    @@ -868,7 +868,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
    # Include InitVars and regular fields (so, not ClassVars).
    flds = [f for f in fields.values()
    if f._field_type in (_FIELD, _FIELD_INITVAR)]
    - _set_new_attribute(cls, '__init__',
    + had_own_init = _set_new_attribute(cls, '__init__',
    _init_fn(flds,
    frozen,
    has_post_init,
    @@ -878,6 +878,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
    '__dataclass_self__' if 'self' in fields
    else 'self',
    ))
    + if not had_own_init:
    + cls.__init__.__module__ = cls.__module__
    + cls.__init__.__generated__ = True

    # Get the fields as a list, and include only real fields. This is
    # used in all of the following methods.
    diff --git a/Lib/typing.py b/Lib/typing.py
    index 445a42492b..3f6a916b6c 100644
    --- a/Lib/typing.py
    +++ b/Lib/typing.py
    @@ -978,7 +978,10 @@ def get_type_hints(obj, globalns=None, localns=None):
    if isinstance(obj, types.ModuleType):
    globalns = obj.__dict__
    else:
    - globalns = getattr(obj, '__globals__', {})
    + if hasattr(obj, '__generated__'):
    + globalns = sys.modules[obj.__module__].__dict__
    + else:
    + globalns = getattr(obj, '__globals__', {})
    if localns is None:
    localns = globalns
    elif localns is None: