Skip to content

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

@oremanj

Description

@oremanj

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions