-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow configuring to exclude lines or files (#3)
- Loading branch information
Showing
10 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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,4 @@ | ||
from fix_future_annotations._main import fix_file | ||
|
||
|
||
__all__ = ["fix_file"] |
This file contains 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,38 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
from pathlib import Path | ||
import re | ||
import sys | ||
|
||
if sys.version_info >= (3, 11): | ||
import tomllib | ||
else: | ||
import tomli as tomllib | ||
|
||
|
||
@dataclass | ||
class Config: | ||
"""Configuration for fix_future_annotations.""" | ||
|
||
# The line patterns(regex) to exclude from the fix. | ||
exclude_lines: list[str] = field(default_factory=list) | ||
# The file patterns(regex) to exclude from the fix. | ||
exclude_files: list[str] = field(default_factory=list) | ||
|
||
@classmethod | ||
def from_file(cls, path: str | Path = "pyproject.toml") -> Config: | ||
"""Load the configuration from a file.""" | ||
try: | ||
with open(path, "rb") as f: | ||
data = tomllib.load(f) | ||
except OSError: | ||
return cls() | ||
else: | ||
return cls(**data.get("tool", {}).get("fix_future_annotations", {})) | ||
|
||
def is_file_excluded(self, file_path: str) -> bool: | ||
return any(re.search(pattern, file_path) for pattern in self.exclude_files) | ||
|
||
def is_line_excluded(self, line: str) -> bool: | ||
return any(re.search(pattern, line) for pattern in self.exclude_lines) |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,19 @@ | ||
from typing import List, Optional, Tuple, Union | ||
|
||
|
||
class NoFix: | ||
def __init__(self, names: List[str]) -> None: | ||
self.names = names | ||
|
||
def lengh(self) -> Optional[int]: | ||
if self.names: | ||
return len(self.names) | ||
return None | ||
|
||
|
||
def foo() -> Union[str, int]: # ffa: ignore | ||
return 42 | ||
|
||
|
||
def bar() -> Tuple[str, int]: | ||
return "bar", 42 |
This file contains 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,21 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List, Optional, Union | ||
|
||
|
||
class NoFix: | ||
def __init__(self, names: List[str]) -> None: | ||
self.names = names | ||
|
||
def lengh(self) -> Optional[int]: | ||
if self.names: | ||
return len(self.names) | ||
return None | ||
|
||
|
||
def foo() -> Union[str, int]: # ffa: ignore | ||
return 42 | ||
|
||
|
||
def bar() -> tuple[str, int]: | ||
return "bar", 42 |
This file contains 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