python / python/mypy

Stubs not found when installed in separate venvs

Open
#19,888 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

We're using mypy with Bazel, which installs dependencies into separate directories and manually places the directories on PYTHONPATH. But it seems like this doesn't play well with protobuf, probably because of the initial google/ directory. This only seems to happen with protobuf, can't reproduce with other libraries.

To Reproduce

class FooProto(google.protobuf.message.Message):
    x: str

@dataclasses.dataclass
class Foo:
    x: str = ""

google.protobuf.message.non_existent()

proto_to_dataclass: Foo = FooProto()
dataclass_to_proto: FooProto = Foo()

See README in https://github.com/brandonchinn178/mypy-repro-protobuf for more details

Expected Behavior

  • Module has no attribute "non_existent"
  • Incompatible types in assignment on proto_to_dataclass
  • Incompatible types in assignment on dataclass_to_proto

Actual Behavior

When types-protobuf and protobuf are installed in the same venv, this works (whether mypy is in that environment or a different one). When they're in different venvs, this does not.

Your Environment

  • Mypy version used: 1.18.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:

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.

Research direction

Start with the README in the linked mypy-repro-protobuf repository and reproduce the setup with protobuf and types-protobuf installed in separate virtual environments, as described in the issue. Trace how mypy discovers the split google/ package and verify the fix by restoring the three expected diagnostics: the missing attribute and both incompatible assignments.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.