Skip to content

[pyrefly] Diagnose directory-relative imports - #4905

Open
WilliamK112 wants to merge 1 commit into
facebook:mainfrom
WilliamK112:codex/implicit-relative-import-4174
Open

[pyrefly] Diagnose directory-relative imports#4905
WilliamK112 wants to merge 1 commit into
facebook:mainfrom
WilliamK112:codex/implicit-relative-import-4174

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

Add a dedicated implicit-relative-import diagnostic when an unqualified import resolves only through FallbackSearchPath::DirectoryRelative. Normal configured roots, build-system sources, typeshed, and site packages retain their existing precedence, while explicitly configured fallback roots remain silent.

The diagnostic is a non-fatal warning by default, so the module continues to resolve while users get guidance to use an explicit relative import or the full absolute package path. This also maps BasedPyright reportImplicitRelativeImport settings to the dedicated error kind, updates the generated preset inventory and migration documentation, and covers resolver precedence plus end-to-end reporting.

Fixes #4174

Test Plan

  • python3 test.py --no-test --no-conformance --no-jsonschema
  • cargo test -p pyrefly_config -- --skip test_find_interpreter_precedence_venv (the skipped host-dependent test selects the globally installed /opt/anaconda3 interpreter)
  • cargo test -p pyrefly_config implicit_relative
  • cargo test -p pyrefly_config test_error_presets_json
  • cargo test -p pyrefly test::state
  • cargo test -p pyrefly module::finder::tests::test_

Imports that resolve only through the directory-relative fallback can work when a file is run directly but fail when it is loaded as part of a package.

Attach a typed, non-fatal diagnostic at that narrow resolver boundary while preserving normal search precedence and keeping explicit fallback roots silent. Map the BasedPyright compatibility rule to the new error kind, document it, and cover resolver, configuration, and end-to-end behavior.

Signed-off-by: Ching-Wei Kang <ckang53@wisc.edu>
@meta-codesync

meta-codesync Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D119718772. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a diagnostic for implicit relative imports

2 participants