Skip to content

Module resolution identity mismatch between installed stub package and local source code #4425

Description

@cmp0xff

Description

When checking local test files against an installed stub-only package (e.g. pandas-stubs), Pyrefly resolves third-party types inconsistently between the two contexts. This leads to false-positive type mismatch errors where the identical underlying type is treated as two separate nominal types.

For example, when both contexts import openpyxl.workbook.workbook.Workbook:

  • The installed stub package resolves it to the source implementation (<site-packages>/openpyxl/workbook/workbook.py at line 51).
  • The local test file resolves it to Pyrefly's bundled typeshed stub (<pyrefly-typeshed>/stubs/openpyxl/openpyxl/workbook/workbook.pyi at line 39).

This causes assert-type and bad-specialization errors because Workbook@39 is not recognized as the same type as Workbook@51.

(Note: See pandas-dev/pandas-stubs#1801 for the original upstream issue where this was identified).

To Reproduce

Using the pandas-stubs repository:

  1. Clone pandas-stubs and install dependencies.
  2. pyrefly_dist is currently disabled in main. Re-enable it by uncommenting _step.pyrefly_dist in scripts/test/__init__.py under _DIST_STEPS.
  3. Run poe pyrefly_dist (this command builds/installs the pandas-stubs wheel, hides the local source directory, and runs pyrefly check tests).

Observed Error:

ERROR assert_type(ExcelWriter[Workbook], ExcelWriter[Workbook]) failed [assert-type]
    --> tests/test_io.py:1360:24

ERROR `openpyxl.workbook.workbook.Workbook@39:7-15` is not assignable to upper bound `OpenDocument | xlsxwriter.workbook.Workbook | openpyxl.workbook.workbook.Workbook@51:7-15` of type variable `_WorkbookT` [bad-specialization]
    --> tests/test_io.py:1360:29

Expected Behavior

Pyrefly should resolve the third-party import consistently to the same path (either both to the typeshed stub or both to the site-packages source) regardless of whether the import originates from an installed package or a local test file, avoiding split-identity errors.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions