Skip to content

Support string type annotations #92

Closed

Description

@ericvsmith

The implementation needs some work with regards to string annotations. This only affects ClassVar and InitVar, since other annotations are ignored.

@dataclass
class C:
   context: "InitVar[context]"
   count: "ClassVar[Counter]"

This is all the more important with regards to PEP 563, "Postponed Evaluation of Annotations".

I'd like to keep the property that typing isn't imported by dataclasses.py, so maybe I'll resort to inspecting the string. I'll probably wait until PEP 563 is finalized before doing anything about this.

Activity

  1. ericvsmith commented on Dec 3, 2017

    @ericvsmith
    OwnerAuthor

    See python-attrs/attrs#265 and python-attrs/attrs#302 for the same issue with attrs.

  2. ilevkivskyi commented on Dec 3, 2017

    @ilevkivskyi
    Contributor

    I think the easiest option is just using typing.get_type_hints() (as recommended by PEP 526). However, importing typing takes some time, but after PEP 560 it will be approx. 7x faster.

  3. ericvsmith commented on Dec 3, 2017

    @ericvsmith
    OwnerAuthor

    My limited understanding of typing.get_type_hints() is that it would still be a problem, in the case of a forward reference (and probably other cases).

    @dataclass
    class A:
        b: "ClassVar[B]"
    
    class B:
        pass
    

    Isn't it true that dataclass(), when processing class A, would get an error when calling typing.get_type_hints() because B isn't defined yet?

  4. ilevkivskyi commented on Dec 3, 2017

    @ilevkivskyi
    Contributor

    Isn't it true that dataclass(), when processing class A, would get an error when calling typing.get_type_hints() because B isn't defined yet?

    I think this is rather a problem with get_type_hints (and it can be easily fixed).

  5. ilevkivskyi commented on Dec 3, 2017

    @ilevkivskyi
    Contributor
  6. ambv commented on Apr 18, 2018

    @ambv

    This needs addressing before the last beta. I will handle that.

  7. ericvsmith commented on May 14, 2018

    @ericvsmith
    OwnerAuthor

    See bpo-33453 for further discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions