classification
Title: dataclasses.InitVar breaks with typing.Optional
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, levkivskyi, miss-islington, samuelcolvin, serhiy.storchaka, xtreak
Priority: normal Keywords: 3.8regression, patch

Created on 2019-10-10 15:31 by samuelcolvin, last changed 2019-10-13 12:50 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16700 closed samuelcolvin, 2019-10-10 16:00
PR 16701 closed serhiy.storchaka, 2019-10-10 16:06
PR 16702 merged samuelcolvin, 2019-10-10 16:09
PR 16744 merged miss-islington, 2019-10-13 11:45
Messages (7)
msg354388 - (view) Author: Samuel Colvin (samuelcolvin) * Date: 2019-10-10 15:31
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](https://travis-ci.org/samuelcolvin/pydantic/jobs/596131963)
msg354389 - (view) Author: Samuel Colvin (samuelcolvin) * Date: 2019-10-10 15:47
This is a bug with the `__repr__` method on `InitVar`.

I'm working on a PR now.
msg354390 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-10-10 15:51
git blame points to https://github.com/python/cpython/commit/01ee12ba35a333e8a6a25c4153c4a21838e9585c . 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.
msg354392 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-10 16:08
Oh, Samuel already made a PR. Then I withdraw PR 16701.
msg354574 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-13 11:45
New changeset 793cb85437299a3da3d74fe65480d720af330cbb by Serhiy Storchaka (Samuel Colvin) in branch 'master':
bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)
https://github.com/python/cpython/commit/793cb85437299a3da3d74fe65480d720af330cbb
msg354576 - (view) Author: miss-islington (miss-islington) Date: 2019-10-13 12:04
New changeset 6da52ac411947d1a7958bbad831fcf8dfc8c95fe by Miss Islington (bot) in branch '3.8':
bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)
https://github.com/python/cpython/commit/6da52ac411947d1a7958bbad831fcf8dfc8c95fe
msg354578 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-13 12:50
Thank you for your contribution Samuel!
History
Date User Action Args
2019-10-13 12:50:54serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg354578

stage: patch review -> resolved
2019-10-13 12:04:08miss-islingtonsetnosy: + miss-islington
messages: + msg354576
2019-10-13 11:45:48miss-islingtonsetpull_requests: + pull_request16321
2019-10-13 11:45:39serhiy.storchakasetmessages: + msg354574
2019-10-11 18:24:00levkivskyisetnosy: + levkivskyi
2019-10-10 16:09:46samuelcolvinsetpull_requests: + pull_request16288
2019-10-10 16:08:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg354392
2019-10-10 16:06:56serhiy.storchakasetpull_requests: + pull_request16287
2019-10-10 16:00:41samuelcolvinsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16286
2019-10-10 15:51:50xtreaksetkeywords: + 3.8regression
nosy: + xtreak
messages: + msg354390

2019-10-10 15:47:32samuelcolvinsetmessages: + msg354389
2019-10-10 15:46:54xtreaksetnosy: + eric.smith
2019-10-10 15:31:02samuelcolvincreate