Skip to content

Hybrid behavior of NG APIs #676

@hynek

Description

@hynek

Originally posted by @euresti in #668 (comment)

I'm working on the mypy plugin but I am gonna need to know the final answer on how "hybrid" mode wants to work if there are both annotations and attribs. I think the options are:

  1. Take only the ones that have = attr.ib()
  2. Take only the annotated ones.
  3. Take both.
  4. Raise an exception.

Hmm. I thought #3 would be easy to implement (just don't raise the error) but it turns out we can't tell the order between annotated and un-annotated attributes.

class A:
   x: int
   y = attr.ib()

cls.__annotations__  ->   [x]
cls.__dict__.keys(). -> [y]

But which is actually first?

#2 is kind of a weird one. Why throw away those attr.ib()s?
#1 is relatively easy to implement. Because it's just a try catch.
#4 Might require the user to add ClassVar in order to add it.

Oh I should note that I think this should still be fine:

@define
class A:
   x: int = field()
   y = field()

We shouldn't force you to have to annotate everything.
Hmm. That might make 4 harder to implement. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ThinkingNeeds more braining.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions