Skip to content

kw_only doesn't work with mypy #473

Closed

Description

@quinor

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 .

Activity

  1. euresti commented on Dec 16, 2018

    @euresti
    Contributor

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

  2. quinor commented on Dec 16, 2018

    @quinor
    Author

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

  3. euresti commented on Dec 17, 2018

    @euresti
    Contributor

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

  4. hynek commented on Dec 18, 2018

    @hynek
    Member

    This should be a bug on the mypy tracker no?

  5. dmfigol commented on Jan 8, 2019

    @dmfigol

    @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

  6. euresti commented on Jan 9, 2019

    @euresti
    Contributor
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions