Skip to content
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

Fix "Cannot determine type of Any" issue in typeshed #12429

Closed
wants to merge 1 commit into from

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Mar 23, 2022

[This was implemented by @sobolevn as part of #12134. Cherry-picking
it to a separate PR since the original PR is still incomplete and the
issue is causing trouble in typeshed.]

Sometimes mypy could generate Cannot determine type of "Any" in
_typeshed/__init__.pyi when running typeshed tests if there is an alias
to Any. This seems to happen only if files are passed in a specific order
to mypy, and seems related to import cycles. Fix the issue by special
casing the inference of type of Any.

This hopefully fixes #12421.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/operations/__init__.py:3: error: Incompatible import of "Any" (imported name has type "object", local name has type "Type[ibis.expr.operations.analytic.Any]")

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/web.py:283: error: Unused "type: ignore[assignment]" comment

@srittau
Copy link
Contributor

srittau commented Mar 23, 2022

Looking at the primer output:

  • The ibis output is a bit weird. ibis/expr/operations/__init__.py star imports from all its sub-modules. Line 1 contains from .analytic import *, which imports a class named Any from that module, which is unrelated to typing.Any. Line 3 contains from .core import *. core.pyi has from typing import Any, ..., but no re-export. So I'm not sure why the star import also imports the conflicting Any.
  • The aiohttp output looks positive:
try:
    from ssl import SSLContext
except ImportError:  # pragma: no cover
    SSLContext = Any  # type: ignore[misc,assignment]

The assignment override isn't necessary anymore.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 23, 2022

Actually, it looks like #12431 may also fix the same issue. I'm going to postpone this until we've validated #12431.

@JukkaL JukkaL marked this pull request as draft March 23, 2022 13:31
@JelleZijlstra
Copy link
Member

core.pyi has from typing import Any, ..., but no re-export. So I'm not sure why the star import also imports the conflicting Any.

I haven't looked at the ibis code myself, but this is what I'd expect if there's no __all__ since it matches the runtime behavior. In stubs we can restrict import * to only explicitly exported names, but at runtime it imports everything that doesn't start with an underscore, so that's what mypy should do too.

@srittau
Copy link
Contributor

srittau commented Mar 23, 2022

Good point. (I wish there was a way to disable that re-export by default behavior in Python, though.)

@AlexWaygood
Copy link
Member

I can confirm that #12431 has fixed all the known instances of this problem over at typeshed 🎉:

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 25, 2022

Closing this since the issue has now been fixed.

@JukkaL JukkaL closed this Mar 25, 2022
@97littleleaf11 97littleleaf11 deleted the fix-any-alias branch March 25, 2022 10:23
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 this pull request may close these issues.

mypy fails for a simple Any type alias`
5 participants