Skip to content

Stubs not found when installed in separate venvs #19888

@brandonchinn178

Description

@brandonchinn178

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:

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