python / python/mypy

Type annotations in class instance attributes on variable definition by execution order vs lexical order

Open
#12,326 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Mypy appears to go top down through a class taking the first mention of an instance variable as the definition, and ignores the order of code execution. It also does not accept a type annotation only existing on a later mention of a variable and will only allow type annotations on the first mention of the variable.

To Reproduce

from typing import NamedTuple, Optional

class Foo(NamedTuple):
    bar: int = 0
    baz: str = ""

class MyClass():
    __slots__ = ['var1', 'var2', 'var3']

    def __init__(self, foo: Optional[Foo] = None) -> None:
        self._reset_state()
        if foo:
            self.var3 = foo

    def _reset_state(self) -> None:
        self.var1: int = 0
        self.var2: bool = False
        self.var3: Foo = Foo()

Expected Behaviour

Mypy recognises the variable is first defined in the _reset_state() method or is only annotated in one place and reports no errors.

Actual Behaviour

$ mypy demo.py 
demo.py:18: error: Attribute "var3" already defined on line 13
Found 1 error in 1 file (checked 1 source file)

If the type annotation is removed from line 18 and moved to either line 13, or added to the class body as an attribute annotation mypy reports no errors

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.6.9
  • Operating system and version: Linux Mint 19.3 Tricia x86_64

I get that this may be the expected/desired behaviour as it may be too complicated otherwise but this was confusing to me. Feel free to close this if you think it is exactly as desired or too hard to "correct"

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 example in demo.py with mypy and confirm the duplicate attribute error for var3. Then trace the type-checking logic for instance attributes and method order, and add a regression test covering the annotation in _reset_state(); done means the example is accepted without errors while existing checks remain correct.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.