-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ruff] Add non-empty-init-module (RUF067)
#22143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e99f4f9
[`ruff`] Add `non-empty-init-module` (`RUF070`)
ntBre 378fea4
add strict option
ntBre d966963
update other snapshots
ntBre e8583d4
fix docs formatting
ntBre 9434c07
cache checker.path.ends_with("__init__.py") checks
ntBre 2ae6b5a
allow __getattr__
ntBre e701272
factor out is_assignment_to, allow __path__ assignment
ntBre 7a4c2c9
fix top-level statement check
ntBre 99559cc
allow type_checking blocks, tidy up a bit
ntBre e42f5ef
allow assignment to __version__ and __dir__ functions
ntBre 78f538d
Update crates/ruff_linter/src/rules/ruff/rules/non_empty_init_module.rs
ntBre 2380330
use SemanticModel::at_top_level
ntBre 437e113
factor out Assignment type, be more strict on extend_path
ntBre 9a6c764
rewrap and expand docs slightly
ntBre de3d010
a few more tests for __path__ changes
ntBre 449b1b4
allow __author__
ntBre 16fc443
revert attribute docstring exception
ntBre 63bb56d
expand the diagnostic a bit, branch on strictness
ntBre d70ba43
sync okay.py
ntBre eb750ac
recode to RUF067
ntBre 353eec2
restore attribute docstring exception, with a comment
ntBre d27406e
Merge branch 'main' into brent/eim
ntBre 1755a1f
improve rule documentation
ntBre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
crates/ruff_linter/resources/test/fixtures/ruff/RUF067/modules/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """This is the module docstring.""" | ||
|
|
||
| # convenience imports: | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| __all__ = ["MY_CONSTANT"] | ||
| __all__ += ["foo"] | ||
| __all__: list[str] = __all__ | ||
| __all__ = __all__ = __all__ | ||
|
|
||
| MY_CONSTANT = 5 | ||
| """This is an important constant.""" | ||
|
|
||
| os.environ["FOO"] = 1 | ||
|
|
||
|
|
||
| def foo(): | ||
| return Path("foo.py") | ||
|
|
||
| def __getattr__(name): # ok | ||
| return name | ||
|
|
||
| __path__ = __import__('pkgutil').extend_path(__path__, __name__) # ok | ||
|
|
||
| if os.environ["FOO"] != "1": # RUF067 | ||
| MY_CONSTANT = 4 # ok, don't flag nested statements | ||
|
|
||
| if TYPE_CHECKING: # ok | ||
| MY_CONSTANT = 3 | ||
|
|
||
| import typing | ||
|
|
||
| if typing.TYPE_CHECKING: # ok | ||
| MY_CONSTANT = 2 | ||
|
|
||
| __version__ = "1.2.3" # ok | ||
|
|
||
| def __dir__(): # ok | ||
| return ["foo"] | ||
|
|
||
| import pkgutil | ||
|
|
||
| __path__ = pkgutil.extend_path(__path__, __name__) # ok | ||
| __path__ = unknown.extend_path(__path__, __name__) # also ok | ||
|
|
||
| # non-`extend_path` assignments are not allowed | ||
| __path__ = 5 # RUF067 | ||
|
|
||
| # also allow `__author__` | ||
| __author__ = "The Author" # ok |
54 changes: 54 additions & 0 deletions
54
crates/ruff_linter/resources/test/fixtures/ruff/RUF067/modules/okay.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| The code here is not in an `__init__.py` file and should not trigger the | ||
| lint. | ||
| """ | ||
|
|
||
| # convenience imports: | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| __all__ = ["MY_CONSTANT"] | ||
| __all__ += ["foo"] | ||
| __all__: list[str] = __all__ | ||
| __all__ = __all__ = __all__ | ||
|
|
||
| MY_CONSTANT = 5 | ||
| """This is an important constant.""" | ||
|
|
||
| os.environ["FOO"] = 1 | ||
|
|
||
|
|
||
| def foo(): | ||
| return Path("foo.py") | ||
|
|
||
| def __getattr__(name): # ok | ||
| return name | ||
|
|
||
| __path__ = __import__('pkgutil').extend_path(__path__, __name__) # ok | ||
|
|
||
| if os.environ["FOO"] != "1": # RUF067 | ||
| MY_CONSTANT = 4 # ok, don't flag nested statements | ||
|
|
||
| if TYPE_CHECKING: # ok | ||
| MY_CONSTANT = 3 | ||
|
|
||
| import typing | ||
|
|
||
| if typing.TYPE_CHECKING: # ok | ||
| MY_CONSTANT = 2 | ||
|
|
||
| __version__ = "1.2.3" # ok | ||
|
|
||
| def __dir__(): # ok | ||
| return ["foo"] | ||
|
|
||
| import pkgutil | ||
|
|
||
| __path__ = pkgutil.extend_path(__path__, __name__) # ok | ||
| __path__ = unknown.extend_path(__path__, __name__) # also ok | ||
|
|
||
| # non-`extend_path` assignments are not allowed | ||
| __path__ = 5 # RUF067 | ||
|
|
||
| # also allow `__author__` | ||
| __author__ = "The Author" # ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.