Skip to content

kw_only doesn't work with mypy #473

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
quinor opened this issue Dec 16, 2018 · 6 comments
Closed

kw_only doesn't work with mypy #473

quinor opened this issue Dec 16, 2018 · 6 comments
Labels

Comments

@quinor
Copy link

quinor commented Dec 16, 2018

Use mypy on following file:

import typing
import attr


@attr.s(frozen=True, auto_attribs=True, kw_only=True)
class Position:
    line: int
    column: int


@attr.s(frozen=True, auto_attribs=True, kw_only=True)
class Node:
    start: typing.Optional[Position] = None
    end: typing.Optional[Position] = None


@attr.s(frozen=True, auto_attribs=True, kw_only=True)
class Operator(Node):
    symbol: str
    name: str
    precedence: int
    associativity: str  # "left" or "right"

It gives following errors:

f.py:21: error: Non-default attributes not allowed after default attributes.
f.py:22: error: Non-default attributes not allowed after default attributes.
f.py:23: error: Non-default attributes not allowed after default attributes.
f.py:24: error: Non-default attributes not allowed after default attributes.

If I understand correctly, mypy doesn't consider the existence of a new kw_only=True .

@euresti
Copy link
Contributor

euresti commented Dec 16, 2018

Hmm. This feature definitely didn't exist when I wrote the plugin. Maybe I'll have time over the break to work on this.

@quinor
Copy link
Author

quinor commented Dec 16, 2018

From my understanding it makes mypy unusable with attrs right now, or is there a workaround that doesn't require too much code?

@wsanchez wsanchez added the Bug label Dec 17, 2018
@euresti
Copy link
Contributor

euresti commented Dec 17, 2018

Sadly, for now the only way around it is to # type: ignore those lines.

@hynek
Copy link
Member

hynek commented Dec 18, 2018

This should be a bug on the mypy tracker no?

@dmfigol
Copy link

dmfigol commented Jan 8, 2019

@euresti
thanks for the mypy plugin!
The latest Pycharm 2018.3.2 still does not like kw_only. Inspection throws an error on the decorator line and whenever an object is created:

  1. Code: @attr.s(auto_attribs=True, repr=False, kw_only=True)
    Pycharm inspection:
Unexpected argument(s). Possible callees: attrs(maybe_cls: None=..., these: Optional[Dict[str, Any]]=..., repr_ns: Optional[str]=..., repr: bool=..., cmp: bool=..., hash: Optional[bool]=..., init: bool=..., slots: bool=..., frozen: bool=..., str: bool=..., auto_attribs: bool=...) attrs(maybe_cls: _C, these: Optional[Dict[str, Any]]=..., repr_ns: Optional[str]=..., repr: bool=..., cmp: bool=..., hash: Optional[bool]=..., init: bool=..., slots: bool=..., frozen: bool=..., str: bool=..., auto_attribs: bool=...)
  1. Code: Car(name="Lotus", year=2001) # object created with attrs + kw_only
    Pycharm inspection:
    Unexpected argument(s). Possible callees: object(self: object) object.__new__(cls: object)

It seems that attrs stub in typeshed is missing kw_only?
.../typeshed/third_party/2and3/attr/__init__.pyi

@overload
def attrs(maybe_cls: _C,
          these: Optional[Dict[str, Any]] = ...,
          repr_ns: Optional[str] = ...,
          repr: bool = ...,
          cmp: bool = ...,
          hash: Optional[bool] = ...,
          init: bool = ...,
          slots: bool = ...,
          frozen: bool = ...,
          str: bool = ...,
          auto_attribs: bool = ...) -> _C: ...
@overload
def attrs(maybe_cls: None = ...,
          these: Optional[Dict[str, Any]] = ...,
          repr_ns: Optional[str] = ...,
          repr: bool = ...,
          cmp: bool = ...,
          hash: Optional[bool] = ...,
          init: bool = ...,
          slots: bool = ...,
          frozen: bool = ...,
          str: bool = ...,
          auto_attribs: bool = ...) -> Callable[[_C], _C]: ...

UPDATE: it seems it is already fixed in the typeshed. I didn't notice, sorry

@euresti
Copy link
Contributor

euresti commented Jan 9, 2019

Fixed here:
python/typeshed#2720
And here:
python/mypy#6107

@euresti euresti closed this as completed Jan 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants