Inconsistent error when aliasing a type from a module without stubs

Open
#2,801 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start with the reproducer in test_mypy.py and run mypy on both versions shown in the issue. Trace how the missing-stub import is handled when the type is referenced directly versus through the Collection alias. Done means the two equivalent annotations produce consistent diagnostics without incorrectly calling the alias an invalid type.

Written by the indexing model from the issue text.

Description

bug false-positive priority-1-normal

test_mypy.py:

import pymongo # this can be any third-party module without stubs

Collection = pymongo.collection.Collection

def func(a: Collection) -> bool:
   return True

Here's the output of mypy test_mypy.py :

test_mypy.py:1: error: No library stub file for module 'pymongo'
test_mypy.py:1: note: (Stub files are from https://github.com/python/typeshed)
test_mypy.py: note: In function "func":
test_mypy.py:5: error: Invalid type "test_mypy.Collection"

However, if I change test_mypy.py to this:

import pymongo

def func(a: pymongo.collection.Collection) -> bool:
  return True

then mypy does not complain about pymongo.collection.Collection being an invalid type. This seems inconsistent/wrong, Collection is not an invalid type, it's just an alias for a type without a stub.

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.