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

mypy gets confused between non-re-exported import and a submodule of the same name #16216

Open
Avasam opened this issue Oct 2, 2023 · 0 comments
Labels
bug mypy got something wrong

Comments

@Avasam
Copy link
Contributor

Avasam commented Oct 2, 2023

Bug Report

This is happening in opencv, relevant issue: opencv/opencv-python#901

Type stub cv2 imports typing
Module cv2.typing also exists
The following raises a mypy error, as expected

import cv2
_ = cv2.typing.MatLike  # Module "cv2" does not explicitly export attribute "typing" Mypy [attr-defined]

(note that this will work at runtime, by that's because cv2 doesn't actually import typing at runtime AND does dynamic imports to expose all its submodules, so mypy is correct here in terms of static type-checking)

The following works, and is accepted by mypy, as expected

import cv2.typing
_ = cv2.typing.MatLike

The issue comes from importing both:

import cv2
import cv2.typing

Note that pyright handles this case correctly.

(A clear and concise description of what the bug is.)

To Reproduce

Download the reproduction, then run mypy . at the root. See error
Run python test.py, see the symbol printed out.
Minimal repro.zip

Expected Behavior

No error in repro

Actual Behavior

PS C:\Users\Avasam\Desktop\Minimal repro> mypy .
test.py:4: error: Module has no attribute "Foo"  [attr-defined]
Found 1 error in 1 file (checked 3 source files)

Your Environment

  • Mypy version used: mypy 1.5.1 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.9.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant