Skip to content

Forward references #265

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

Closed
Tinche opened this issue Oct 13, 2017 · 6 comments · Fixed by #302
Closed

Forward references #265

Tinche opened this issue Oct 13, 2017 · 6 comments · Fixed by #302

Comments

@Tinche
Copy link
Member

Tinche commented Oct 13, 2017

Hello, there's some food for thought. Imagine a class referencing itself.

@attr.s
class A:
    a: 'A' = attr.ib()

This is the proper way of doing this as per mypy. In the case of attrs though:

>>> attr.fields(A).a
Attribute(name='a', default=NOTHING, validator=None, repr=True, cmp=True, hash=None, init=True, convert=None, metadata=mappingproxy({}), type='A')

The string just gets copied into the type attribute. A.__annotations__ has the same problem. Ideally the type would be the actual class object. Mypy deals with this by having more than one pass when parsing.

The first question to answer if whether we should deal with this here, in attrs, or let code making use of the type attribute deal with it. Maybe instead of expecting higher levels to deal with this, Python itself should handle it (although good luck waiting for this :). I think it's worth trying to deal with this problem in attrs, to make it easier for others.

Dealing with just a class referencing itself is very doable. Now imagine circular references.

@attr.s
class A:
    b: 'B' = attr.ib()

@attr.s
class B:
    a: A = attr.ib()

Not really sure what to do here.

@Tinche
Copy link
Member Author

Tinche commented Oct 13, 2017

This issue was initially motivated by https://github.com/Tinche/cattrs/issues/13.

@wsanchez
Copy link

GitHub doesn't have enough Emojis for this one.

euresti pushed a commit to euresti/attrs that referenced this issue Nov 29, 2017
This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes python-attrs#265
euresti pushed a commit to euresti/attrs that referenced this issue Nov 29, 2017
This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes python-attrs#265
@euresti euresti mentioned this issue Nov 29, 2017
6 tasks
euresti pushed a commit to euresti/attrs that referenced this issue Nov 29, 2017
This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes python-attrs#265
@vodik
Copy link

vodik commented Sep 17, 2018

As I understand it, and especially with PEP 563 in the picture, I'd argue the current behaviour is the most correct one. Especially as performance was one of the motivations for the change:

type hints are executed at module import time, which is not computationally free

typing.get_type_hints() already works as expected on attr classes.

@hynek
Copy link
Member

hynek commented Oct 24, 2018

Is this still relevant with from __future__ import annotations @Tinche? My gut feeling is that if we start doing our own thing, it’s gonna fall on our feet.

@Tinche
Copy link
Member Author

Tinche commented Oct 24, 2018

Yeah, I haven't really looked into this in depth again yet but I think this is out of scope for attrs now.

@hynek hynek closed this as completed Oct 25, 2018
euresti pushed a commit to euresti/attrs that referenced this issue Jul 14, 2020
This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes python-attrs#265
euresti pushed a commit to euresti/attrs that referenced this issue Jul 21, 2020
This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes python-attrs#265
hynek pushed a commit that referenced this issue Jul 22, 2020
* Add attr.resolve_types

This adds `attr.resolve_types` which can be used to resolve forward declarations in classes created using `__annotations__`

Fixes #265

* Fix flake8 and doctest

* Missed one

* Fix lint

* Address CR comments
Add to stubs
Make it a decorator, because why not?

* Fix test

* Rerun-tests please

* Rerun-tests please

* Fix another

* Fix lint

Co-authored-by: David Euresti <[email protected]>
@hynek
Copy link
Member

hynek commented Jul 22, 2020

FYI we have attr.resolve_types() now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants