python / python/mypy

mypy resolves symbol incorrectly with builtins import cycle

Open
#11,535 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-import-cycles
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Context: https://github.com/python/typeshed/pull/6289

To reproduce:

  1. check out typeshed
  2. git checkout 9eabedca5f47a710f29d6e3e2b3a389853fab5aa
  3. apply this patch
diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi
index 2393f3c9..0d2ea882 100644
--- a/stdlib/builtins.pyi
+++ b/stdlib/builtins.pyi
@@ -717,6 +717,7 @@ class bool(int):
     def __rxor__(self, __x: int) -> int: ...
     def __getnewargs__(self) -> tuple[int]: ...
 
+@final
 class slice(object):
     start: Any
     step: Any

Then run:

mypy --python-version 3.10 --no-incremental --custom-typeshed-dir . stdlib/_ast.pyi stdlib/ast.pyi 

and note that mypy incorrectly resolves slice to builtins.slice instead of ast.slice in ast.pyi

Note that if you change the order of files, it works just fine:

mypy --python-version 3.10 --no-incremental --custom-typeshed-dir . stdlib/ast.pyi stdlib/_ast.pyi 

It's a pretty weird case, import cycle with builtins, collision with name in builtins, from _ast import * in ast.pyi, etc. But a scary kind of bug :-)

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 with the reproduction using stdlib/builtins.pyi, stdlib/_ast.pyi, and stdlib/ast.pyi at commit 9eabedca5f47a710f29d6e3e2b3a389853fab5aa. Run both mypy commands with the two file orders and trace why ast.pyi resolves slice to builtins.slice; done means the reported order-dependent resolution is corrected without relying on file order.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.