Skip to content

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

@Avasam

Description

@Avasam

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions