Skip to content
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

Use re.Pattern for type checking #813

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Internal
- Update ``darkgray-dev-tools`` for Pip >= 24.1 compatibility.
- Drop dependency on ``pip`` and ``distutils`` in the weekly CI "future" test. Use
``packaging`` instead.
- No need to use `typing.Pattern` instead of `re.Pattern` in Python 3.9+.


2.1.1_ - 2024-04-16
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Setuptools entry point for Darker"""

import re
from typing import Pattern

from setuptools import setup

SIDEBAR_RE = re.compile(r"\+---.*:alt: Support", re.DOTALL)
CONTRIBUTORS_RE = re.compile(r"""\nThanks goes .*\nThis project follows """, re.DOTALL)


def replace(name: str, regex: Pattern[str], replacement: str, content: str) -> str:
def replace(name: str, regex: re.Pattern[str], replacement: str, content: str) -> str:
"""Replace/remove a section from the package description, based on a regex

Raise an exception if the regular expression doesn't match anything.
Expand Down
10 changes: 5 additions & 5 deletions src/darker/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import re
from functools import lru_cache
from typing import TYPE_CHECKING, Collection, Iterable, Iterator, Pattern
from typing import TYPE_CHECKING, Collection, Iterable, Iterator

from darkgraylib.files import find_project_root

Expand Down Expand Up @@ -74,7 +74,7 @@ def _resolves_outside_root_or_cannot_stat(path: Path, root: Path) -> bool:

def _path_is_excluded(
normalized_path: str,
pattern: Pattern[str] | None,
pattern: re.Pattern[str] | None,
) -> bool:
"""Return whether the path is excluded by the pattern.

Expand All @@ -88,9 +88,9 @@ def _path_is_excluded(
def _gen_python_files(
paths: Iterable[Path],
root: Path,
exclude: Pattern[str],
extend_exclude: Pattern[str] | None,
force_exclude: Pattern[str] | None,
exclude: re.Pattern[str],
extend_exclude: re.Pattern[str] | None,
force_exclude: re.Pattern[str] | None,
) -> Iterator[Path]:
"""Generate all files under ``path`` whose paths are not excluded.

Expand Down
3 changes: 2 additions & 1 deletion src/darker/formatters/base_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Generic, Pattern, TypeVar
from typing import TYPE_CHECKING, Generic, TypeVar

from darker.files import find_pyproject_toml
from darker.formatters.formatter_config import FormatterConfig

if TYPE_CHECKING:
from argparse import Namespace
from pathlib import Path
from re import Pattern

from darkgraylib.utils import TextDocument

Expand Down
2 changes: 1 addition & 1 deletion src/darker/formatters/black_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
if TYPE_CHECKING:
from argparse import Namespace
from pathlib import Path
from typing import Pattern
from re import Pattern

from black import FileMode as Mode
from black import TargetVersion
Expand Down
3 changes: 2 additions & 1 deletion src/darker/formatters/formatter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Iterable, Pattern, TypedDict
from typing import TYPE_CHECKING, Iterable, TypedDict

from darkgraylib.config import ConfigurationError

if TYPE_CHECKING:
from argparse import Namespace
from re import Pattern


class FormatterConfig(TypedDict):
Expand Down
3 changes: 2 additions & 1 deletion src/darker/formatters/none_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Pattern
from typing import TYPE_CHECKING

from darker.formatters.base_formatter import BaseFormatter

if TYPE_CHECKING:
from argparse import Namespace
from pathlib import Path
from re import Pattern

from darkgraylib.utils import TextDocument

Expand Down
2 changes: 1 addition & 1 deletion src/darker/formatters/ruff_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

if TYPE_CHECKING:
from argparse import Namespace
from typing import Pattern
from re import Pattern

logger = logging.getLogger(__name__)

Expand Down
Loading