Skip to content

Auto-detect same-package imports#1266

Merged
charliermarsh merged 1 commit intomainfrom
charlie/first-party-detection
Dec 17, 2022
Merged

Auto-detect same-package imports#1266
charliermarsh merged 1 commit intomainfrom
charlie/first-party-detection

Conversation

@charliermarsh
Copy link
Member

Resolves #1257.

@charliermarsh charliermarsh force-pushed the charlie/first-party-detection branch from 0a48575 to ec899ff Compare December 17, 2022 02:05
@charliermarsh
Copy link
Member Author

\cc @smackesey

Just as an example: if I run cargo run dagster/examples/assets_pandas_type_metadata/assets_pandas_type_metadata/repository.py --select I --verbose --no-cache on f2b8d88, I see the following in the logs:

...
[2022-12-16][21:07:23][ruff::isort::categorize][DEBUG] Categorized 'assets_pandas_type_metadata' as FirstParty (SamePackage)
[2022-12-16][21:07:23][ruff::isort::categorize][DEBUG] Categorized 'dagster' as FirstParty (KnownFirstParty)
[2022-12-16][21:07:23][ruff::isort::categorize][DEBUG] Categorized 'assets_pandas_type_metadata' as FirstParty (SamePackage)
...

@charliermarsh charliermarsh merged commit ecf0dd0 into main Dec 17, 2022
@charliermarsh charliermarsh deleted the charlie/first-party-detection branch December 17, 2022 02:19
charliermarsh added a commit that referenced this pull request Aug 24, 2023
## Summary

Our first-party import detection uses a heuristic that doesn't exist in
isort: if an import appears to be from within the same package as the
containing file, we mark it as first-party. For example, if you have a
directory `./foo/__init__.py`, and you import `from foo import bar` in
`./foo/baz.py`, we'll mark that as first-party. (See:
#1266.)

This is often unnecessary, and arguably should be removed (though it
does have some important use-cases that are otherwise unserved -- I
believe Dagster uses it to ensure that all packages mark imports from
within the same package as first-party, but not imports _across_
different first-party packages)... but it does exist, and it does help
in cases in which the `src` field is not properly configured.

This PR adds an option to turn off this behavior:

```toml
[tool.ruff.isort]
detect-same-package = false
```

This is being introduced to help codebases migrating over from isort
that may want more consistent behavior with their current sorting.

## Test Plan

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatically infer first-party imports based on "current" module path

1 participant