Skip to content

Spurious errors on the builtin open when running mypy on mypy #14796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gmc444-b opened this issue Feb 14, 2023 · 5 comments · Fixed by #15161
Open

Spurious errors on the builtin open when running mypy on mypy #14796

gmc444-b opened this issue Feb 14, 2023 · 5 comments · Fixed by #15161

Comments

@gmc444-b
Copy link
Contributor

gmc444-b commented Feb 14, 2023

When running mypy following the procedure below, I encounter several errors for the overloaded open builtin of the form:

../python3.11/site-packages/mypy/typeshed/stdlib/builtins.pyi:1327: error: Argument 8 to "open" becomes "Optional[Any]" due to an unfollowed import  [no-any-unimported]

To reproduce:

  1. Set up a virtualenv with Python 3.11 and activate it.
  2. pip install mypy
  3. python-m mypy --strict --no-silence-site-packages --disallow-any-unimported -m mypy

Both --no-silence-site-packages and --disallow-any-unimported need to be present.

Version 1.0.0 of mypy is required. Older versions don't exhibit this error.

This is the simplest way we have found to reproduce this problem, but we first encountered it in one of our own projects.

The issue is being filed here because the mypy docs for reporting bugs says that problems with a specific library function should be reported here. We're not seeing this error with any other functions.

@AlexWaygood
Copy link
Member

Interesting. I can reproduce this.

For reference argument 8 of builtins.open is this:

https://github.com/python/typeshed/blob/9b63a49f6419bc7a04cf0d2be0433c255f1b1852/stdlib/builtins.pyi#L1500

And the _Opener type alias is defined in builtins.pyi here:

https://github.com/python/typeshed/blob/9b63a49f6419bc7a04cf0d2be0433c255f1b1852/stdlib/builtins.pyi#L1488

I suspect that this issue may be caused by a cyclic import between typing_extensions and builtins. If I apply this diff to builtins.pyi in site-packages/mypy/typeshed, then the error goes away:

--- a/stdlib/builtins.pyi
+++ b/stdlib/builtins.pyi
@@ -1485,7 +1485,7 @@ def next(__i: SupportsNext[_T]) -> _T: ...
 def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
 def oct(__number: int | SupportsIndex) -> str: ...

-_Opener: TypeAlias = Callable[[str, int], int]
+_Opener = Callable[[str, int], int]

@AlexWaygood
Copy link
Member

This bisects to 6442b02: I can reproduce with 6442b02 checked out locally, but not with 0665ce9.

@AlexWaygood
Copy link
Member

@JelleZijlstra or @hauntsaninja, could one of you transfer this issue to mypy?

I think the best thing to do would be to revert 6442b02. It fixes the crash that it was meant to fix, but I get some very strange mypy errors if I try changing builtins.pyi in typeshed so that it imports things from collections.abc instead of typing, so something is clearly going wrong there. Plus the crash is very typeshed-specific, so it's not really high-priority.

Unfortunately it's not a clean revert.

@JelleZijlstra JelleZijlstra transferred this issue from python/typeshed Feb 27, 2023
@AlexWaygood
Copy link
Member

Cc. @Michael0x2a as author of the commit that this bisects to

@hauntsaninja
Copy link
Collaborator

Re-opening since I reverted #15161 because it was causing further typeshed issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants