Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataclasses.InitVar breaks with typing.Optional #82612

Closed
samuelcolvin mannequin opened this issue Oct 10, 2019 · 7 comments
Closed

dataclasses.InitVar breaks with typing.Optional #82612

samuelcolvin mannequin opened this issue Oct 10, 2019 · 7 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@samuelcolvin
Copy link
Mannequin

samuelcolvin mannequin commented Oct 10, 2019

BPO 38431
Nosy @ericvsmith, @serhiy-storchaka, @ilevkivskyi, @samuelcolvin, @miss-islington, @tirkarthi
PRs
  • bpo-38431: fix dataclasses.InitVar.__repr__ #16700
  • bpo-38431: Support non-class types in InitVar. #16701
  • bpo-38431: fix dataclasses.InitVar.__repr__ #16702
  • [3.8] bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702) #16744
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-13.12:50:54.195>
    created_at = <Date 2019-10-10.15:31:02.114>
    labels = ['3.8', 'type-bug', 'library']
    title = 'dataclasses.InitVar breaks with typing.Optional'
    updated_at = <Date 2019-10-13.12:50:54.193>
    user = 'https://github.com/samuelcolvin'

    bugs.python.org fields:

    activity = <Date 2019-10-13.12:50:54.193>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-13.12:50:54.195>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2019-10-10.15:31:02.114>
    creator = 'samuelcolvin'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38431
    keywords = ['patch', '3.8regression']
    message_count = 7.0
    messages = ['354388', '354389', '354390', '354392', '354574', '354576', '354578']
    nosy_count = 6.0
    nosy_names = ['eric.smith', 'serhiy.storchaka', 'levkivskyi', 'samuelcolvin', 'miss-islington', 'xtreak']
    pr_nums = ['16700', '16701', '16702', '16744']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38431'
    versions = ['Python 3.8']

    @samuelcolvin
    Copy link
    Mannequin Author

    samuelcolvin mannequin commented Oct 10, 2019

    The following code works fine with python 3.7 but breaks with 3.8:

    import dataclasses
    from typing import Optional
    
    @dataclasses.dataclass
    class TestingDataclass:
        base_path: dataclasses.InitVar[Optional[str]] = None
    

    Exception traceback:

    Traceback (most recent call last):
      File "test.py", line 6, in <module>
        class TestingDataclass:
      File "/usr/local/lib/python3.8/dataclasses.py", line 995, in dataclass
        return wrap(cls)
      File "/usr/local/lib/python3.8/dataclasses.py", line 987, in wrap
        return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
      File "/usr/local/lib/python3.8/dataclasses.py", line 967, in _process_class
        str(inspect.signature(cls)).replace(' -> None', ''))
      File "/usr/local/lib/python3.8/inspect.py", line 3050, in __str__
        formatted = str(param)
      File "/usr/local/lib/python3.8/inspect.py", line 2568, in __str__
        formatannotation(self._annotation))
      File "/usr/local/lib/python3.8/inspect.py", line 1202, in formatannotation
        return repr(annotation)
      File "/usr/local/lib/python3.8/dataclasses.py", line 213, in __repr__
        return f'dataclasses.InitVar[{self.type.__name__}]'
      File "/usr/local/lib/python3.8/typing.py", line 757, in __getattr__
        raise AttributeError(attr)
    AttributeError: __name__
    

    The code runs fine with str instead of Optional[str].

    Tested locally with Python 3.8.0rc1 (tags/v3.8.0rc1:34214de6ab, Oct 10 2019, 16:15:14). The same error can be seen (in a more involved context) on travis here

    @samuelcolvin samuelcolvin mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 10, 2019
    @samuelcolvin
    Copy link
    Mannequin Author

    samuelcolvin mannequin commented Oct 10, 2019

    This is a bug with the __repr__ method on InitVar.

    I'm working on a PR now.

    @tirkarthi
    Copy link
    Member

    git blame points to 01ee12b . I am tagging this as a 3.8 regression since the commit was not backported and 3.7 works fine. Feel free to remove it if it's a mistake.

    @serhiy-storchaka
    Copy link
    Member

    Oh, Samuel already made a PR. Then I withdraw PR 16701.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 793cb85 by Serhiy Storchaka (Samuel Colvin) in branch 'master':
    bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)
    793cb85

    @miss-islington
    Copy link
    Contributor

    New changeset 6da52ac by Miss Islington (bot) in branch '3.8':
    bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)
    6da52ac

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your contribution Samuel!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants