python / python/mypy

indirectly decorated attr.s classes are not recognized

Open
#6,143 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-attrs
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I'd like mypy to recognize attr.s decorated classes by "indirect" means.

To understand what I mean, consider the examples in the snippet below:

import attr

# example 1 (works with mypy)

@attr.s(auto_attribs=True, frozen=True)
class DirectlyDecorated:
    name: str


d = DirectlyDecorated('foo')  # no mypy error



# --- end example 1

# example 2 (does not work with mypy)

my_decorator = attr.s(auto_attribs=True, frozen=True)

@my_decorator
class IndirectlyDecorated:
    name: str


i = IndirectlyDecorated('bar')  # error: Too many arguments for "IndirectlyDecorated"

# --- end example 2


# example 3 (does not work with mypy)

class MyMeta(type):
    def __new__(cls, name, bases, dct):
        newcls = super().__new__(cls, name, bases, dct)
        newcls = my_decorator(newcls)
        return newcls


class MetaDecorated(metaclass=MyMeta):
    name: str


m = MetaDecorated('baz')  # error: Too many arguments for "MetaDecorated"

# --- end example 3

Running git master version of mypy as of today, with no arguments, I get:

snippet.py:25: error: Too many arguments for "IndirectlyDecorated"
snippet.py:43: error: Too many arguments for "MetaDecorated"

So, example 1 works as expected. Examples 2 and 3 do not work:

The same happens when using kw_only=True (that is example1 with kw_only=True works, but neither examples 2 nor 3 work with kw_only=True). In that case, the error reported is, of course,

snippet.py:25: error: Unexpected keyword argument "name" for "IndirectlyDecorated"
snippet.py:43: error: Unexpected keyword argument "name" for "MetaDecorated"

I'd like mypy to recognize the above cases so that example 2 and 3 work (with both kw_only=True and kw_only=False)

Thank you

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the snippet.py examples and running mypy on the direct, indirect, and metaclass decoration cases. Trace how mypy recognizes attr.s and compare that with the indirect decorator and metaclass paths. Done means examples 2 and 3 no longer report constructor argument errors, including the kw_only variants.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.