python / python/mypy

Feature request for something like --disallow-any-attrs

Open
#8,670 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Disclaimer: mypy newbie here, so I might just be missing something. See also my StackOverflow question for my motivation to request this feature.

Basically I couldn't find a way for mypy to require type annotations on class attributes as in the following (incomplete) example with ignore_missing_imports turned on for sqlalchemy.*.

from sqlalchemy import Column, Integer

class Foo(Base):
    oid = Column(Integer, primary_key=True)

I want code using my library (ORM definitions) to know what types they can expect. So I need mypy to make sure that oid is type annotated (e.g. oid: int = ...).

disallow_any_unimported does not seem to apply for oid. I also tried disallow_any_expr but this seems far to broad and has side-effects like causing mypy to complain about the use of Integer (Even with disallow_any_unimported = False).

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 comparing the requested behavior for class attributes with the existing disallow_any_unimported and disallow_any_expr options, using the SQLAlchemy Foo example as the motivating case. Determine how a separate option should avoid the broader side effects described for disallow_any_expr, then verify that annotated and unannotated attributes produce the intended diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.