python / python/mypy

Bad error message: mypy reports 'Name is not defined' when using an object member as super class of another class

Open
#8,603 25 comments 27 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good-first-issue priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Consider the following valid Python 3 code:

class A():
    pass

class B():
    def __init__(self):
        self.a = A

b = B()

class C(b.a):
    pass

Actual behavior:
Mypy will report Name 'b.a' is not defined on the line class C(b.a):.

Expected behavior:
No error reported by Mypy.

Python version: 3.8.2
Mypy version: 0.770 (master not checked)
Flags: no flags

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 reported Python snippet with mypy and trace how the type checker handles an object member used in a class base expression. The issue is done when this valid code produces no 'Name is not defined' error and a regression test covers the behavior.

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
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.