python / python/mypy

Spurious “cyclic definition” error when inheriting from and shadowing a class

Open
#10,958 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Shadowing class names and inheriting from the previous class causes the following error:

Cannot resolve name "{shadowed class name}" (possible cyclic definition)

To Reproduce

Attempt to typecheck the following code:

class MyError(Exception): pass
class RuntimeError(RuntimeError, MyError): pass

Expected Behavior

Accepted by Mypy, RuntimeError can now be imported from that file and refers to a subclass of MyError.

Actual Behavior

file.py: 2: error: Cannot resolve name "RuntimeError" (possible cyclic definition)

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.9
  • Operating system and version: NixOS unstable

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 error with the two-class Python snippet under mypy 0.812, then trace how class base names are resolved when a class shadows an inherited name. Done means mypy accepts the example without the cyclic-definition error and the resulting RuntimeError remains importable as a subclass of MyError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.