Skip to content

getattr() type inference fails with empty container as third argument #11572

Closed
@JukkaL

Description

@JukkaL

This code requires a type annotation, which is a regression from mypy 0.910:

x = getattr(object(), 'a', [])  # Need type annotation for "x"

The signature of getattr was changed relatively recently, and it causes the issue. The new signature is better than old one, but this case got worse.

I'm not sure what would be the best way to deal with this, but there are at least these plausible options:

  1. Implement a plugin hook that deals with getattr.
  2. Somehow improve type inference in this case.
  3. Modify the signature of getattr to make the problem go away by providing e.g. Iterable[Any] as the context in an overload variant. This would be pretty ad hoc though.

This is not blocking the 0.920 release, since the workaround is straightforward and this doesn't seem to come up very often.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions