on-internet
← Community

Why this `attr` class adds `Any` to its attributes' type annotations?

Stack Overflow ·

I have this attr class: import attr @attr.s class Foo: a = attr.ib() # type: int b = attr.ib() # type: int f = Foo(1, 2) print(f.a, f.b) I have latest Pyright and Python 3.10. Pyright warns Error: Expected 0 positional arguments in the line Foo(1, 2). Therefore, I added a type-checking-only __init__: @attr.s class Foo: a = attr.ib() # type: int b = attr.ib() # type: int if TYPE_CHECKING: def __init__(self, a: int, b: int): ... Warning disappears. f.a and f.b are correctly type-revealed as int. Now, I realised there is another way of writing the class: @attr.s class Foo: a = attr.ib() b = attr.

Comments

Sign in or sign up to comment and upvote.

No comments yet.