python / python/mypy

Can't forward-reference an inner class when the outer class's base is also a forward reference

Open
#8,130 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The following construct type-checked on mypy 0.650, but complains of an undefined name on 0.750. Sorry not to have a more precise range; I'm guessing the regression arrived with the new semantic analyzer.

class Outer(Base):
  field: Outer.Inner  # E: Name 'Outer.Inner' is not defined
  class Inner: pass
class Base: pass

All of the following are accepted, though:

# inner class before its use
class Outer(Base):
  class Inner: pass
  field: Outer.Inner
class Base: pass

# base before its use
class Base: pass
class Outer(Base):
  field: Outer.Inner
  class Inner: pass

# no base
class Outer:
  field: Outer.Inner
  class Inner: pass

Behavior is the same in all cases if I make the field annotation say just Inner rather than Outer.Inner. (IIRC I was using Outer.Inner because that made old mypy happier.)

This is mypy v0.750 on Python 3.6.5.

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 minimal examples with mypy 0.750 and compare them with the reported 0.650 behavior. Trace the semantic analyzer's handling of the outer class base and the Outer.Inner annotation. Done means the forward-reference construct type-checks without an undefined-name error while the accepted examples remain valid.

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.