Skip to content

Commit d6d33de

Browse files
chore: enable pep 563/585 (#281)
1 parent 65ddc9e commit d6d33de

File tree

112 files changed

+709
-643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+709
-643
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ repos:
5757
rev: 5.10.1
5858
hooks:
5959
- id: isort
60+
args: [--add-import, from __future__ import annotations]
6061
exclude: |
6162
(?x)(
6263
^.*/?setup\.py$

src/poetry/core/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import sys
24

35
from pathlib import Path

src/poetry/core/exceptions/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from poetry.core.exceptions.base import PoetryCoreException
24

35

src/poetry/core/exceptions/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
from __future__ import annotations
2+
3+
14
class PoetryCoreException(Exception):
25
pass

src/poetry/core/factory.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging
24

35
from pathlib import Path
@@ -6,7 +8,6 @@
68
from typing import Dict
79
from typing import List
810
from typing import Mapping
9-
from typing import Optional
1011
from typing import Union
1112
from warnings import warn
1213

@@ -37,8 +38,8 @@ class Factory:
3738
"""
3839

3940
def create_poetry(
40-
self, cwd: Optional[Path] = None, with_groups: bool = True
41-
) -> "Poetry":
41+
self, cwd: Path | None = None, with_groups: bool = True
42+
) -> Poetry:
4243
from poetry.core.poetry import Poetry
4344
from poetry.core.pyproject.toml import PyProjectTOML
4445

@@ -65,17 +66,17 @@ def create_poetry(
6566
return Poetry(poetry_file, local_config, package)
6667

6768
@classmethod
68-
def get_package(cls, name: str, version: str) -> "ProjectPackage":
69+
def get_package(cls, name: str, version: str) -> ProjectPackage:
6970
from poetry.core.packages.project_package import ProjectPackage
7071

7172
return ProjectPackage(name, version, version)
7273

7374
@classmethod
7475
def _add_package_group_dependencies(
7576
cls,
76-
package: "ProjectPackage",
77-
group: Union[str, "DependencyGroup"],
78-
dependencies: "DependencyConfig",
77+
package: ProjectPackage,
78+
group: str | DependencyGroup,
79+
dependencies: DependencyConfig,
7980
) -> None:
8081
if isinstance(group, str):
8182
if package.has_dependency_group(group):
@@ -109,11 +110,11 @@ def _add_package_group_dependencies(
109110
@classmethod
110111
def configure_package(
111112
cls,
112-
package: "ProjectPackage",
113-
config: Dict[str, Any],
113+
package: ProjectPackage,
114+
config: dict[str, Any],
114115
root: Path,
115116
with_groups: bool = True,
116-
) -> "ProjectPackage":
117+
) -> ProjectPackage:
117118
from poetry.core.packages.dependency import Dependency
118119
from poetry.core.packages.dependency_group import DependencyGroup
119120
from poetry.core.spdx.helpers import license_by_id
@@ -131,7 +132,7 @@ def configure_package(
131132
package.repository_url = config.get("repository")
132133
package.documentation_url = config.get("documentation")
133134
try:
134-
license_: Optional["License"] = license_by_id(config.get("license", ""))
135+
license_: License | None = license_by_id(config.get("license", ""))
135136
except ValueError:
136137
license_ = None
137138

@@ -220,10 +221,10 @@ def configure_package(
220221
def create_dependency(
221222
cls,
222223
name: str,
223-
constraint: "DependencyConstraint",
224-
groups: Optional[List[str]] = None,
225-
root_dir: Optional[Path] = None,
226-
) -> "DependencyTypes":
224+
constraint: DependencyConstraint,
225+
groups: list[str] | None = None,
226+
root_dir: Path | None = None,
227+
) -> DependencyTypes:
227228
from poetry.core.packages.constraints import (
228229
parse_constraint as parse_generic_constraint,
229230
)
@@ -335,7 +336,7 @@ def create_dependency(
335336
)
336337

337338
if not markers:
338-
marker: "BaseMarker" = AnyMarker()
339+
marker: BaseMarker = AnyMarker()
339340
if python_versions:
340341
marker = marker.intersect(
341342
parse_marker(
@@ -367,14 +368,14 @@ def create_dependency(
367368

368369
@classmethod
369370
def validate(
370-
cls, config: Dict[str, Any], strict: bool = False
371-
) -> Dict[str, List[str]]:
371+
cls, config: dict[str, Any], strict: bool = False
372+
) -> dict[str, list[str]]:
372373
"""
373374
Checks the validity of a configuration
374375
"""
375376
from poetry.core.json import validate_object
376377

377-
result: Dict[str, List[str]] = {"errors": [], "warnings": []}
378+
result: dict[str, list[str]] = {"errors": [], "warnings": []}
378379
# Schema validation errors
379380
validation_errors = validate_object(config, "poetry-schema")
380381

@@ -428,7 +429,7 @@ def validate(
428429
return result
429430

430431
@classmethod
431-
def locate(cls, cwd: Optional[Path] = None) -> Path:
432+
def locate(cls, cwd: Path | None = None) -> Path:
432433
cwd = Path(cwd or Path.cwd())
433434
candidates = [cwd]
434435
candidates.extend(cwd.parents)

src/poetry/core/json/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35

46
from typing import Any
5-
from typing import Dict
6-
from typing import List
77

88

99
SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")
@@ -14,7 +14,7 @@ class ValidationError(ValueError):
1414
pass
1515

1616

17-
def validate_object(obj: Dict[str, Any], schema_name: str) -> List[str]:
17+
def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:
1818
schema = os.path.join(SCHEMA_DIR, f"{schema_name}.json")
1919

2020
if not os.path.exists(schema):

src/poetry/core/masonry/api.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""
22
PEP-517 compliant buildsystem API
33
"""
4+
from __future__ import annotations
5+
46
import logging
57

68
from pathlib import Path
79
from typing import Any
8-
from typing import Dict
9-
from typing import List
10-
from typing import Optional
1110

1211
from poetry.core.factory import Factory
1312
from poetry.core.masonry.builders.sdist import SdistBuilder
@@ -18,8 +17,8 @@
1817

1918

2019
def get_requires_for_build_wheel(
21-
config_settings: Optional[Dict[str, Any]] = None,
22-
) -> List[str]:
20+
config_settings: dict[str, Any] | None = None,
21+
) -> list[str]:
2322
"""
2423
Returns an additional list of requirements for building, as PEP508 strings,
2524
above and beyond those specified in the pyproject.toml file.
@@ -36,7 +35,7 @@ def get_requires_for_build_wheel(
3635

3736

3837
def prepare_metadata_for_build_wheel(
39-
metadata_directory: str, config_settings: Optional[Dict[str, Any]] = None
38+
metadata_directory: str, config_settings: dict[str, Any] | None = None
4039
) -> str:
4140
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)
4241
builder = WheelBuilder(poetry)
@@ -59,8 +58,8 @@ def prepare_metadata_for_build_wheel(
5958

6059
def build_wheel(
6160
wheel_directory: str,
62-
config_settings: Optional[Dict[str, Any]] = None,
63-
metadata_directory: Optional[str] = None,
61+
config_settings: dict[str, Any] | None = None,
62+
metadata_directory: str | None = None,
6463
) -> str:
6564
"""Builds a wheel, places it in wheel_directory"""
6665
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)
@@ -69,7 +68,7 @@ def build_wheel(
6968

7069

7170
def build_sdist(
72-
sdist_directory: str, config_settings: Optional[Dict[str, Any]] = None
71+
sdist_directory: str, config_settings: dict[str, Any] | None = None
7372
) -> str:
7473
"""Builds an sdist, places it in sdist_directory"""
7574
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)
@@ -81,8 +80,8 @@ def build_sdist(
8180

8281
def build_editable(
8382
wheel_directory: str,
84-
config_settings: Optional[Dict[str, Any]] = None,
85-
metadata_directory: Optional[str] = None,
83+
config_settings: dict[str, Any] | None = None,
84+
metadata_directory: str | None = None,
8685
) -> str:
8786
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)
8887

src/poetry/core/masonry/builder.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24
from typing import TYPE_CHECKING
3-
from typing import Optional
4-
from typing import Union
55

66

77
if TYPE_CHECKING:
88
from poetry.core.poetry import Poetry
99

1010

1111
class Builder:
12-
def __init__(self, poetry: "Poetry") -> None:
12+
def __init__(self, poetry: Poetry) -> None:
1313
from poetry.core.masonry.builders.sdist import SdistBuilder
1414
from poetry.core.masonry.builders.wheel import WheelBuilder
1515

@@ -20,7 +20,7 @@ def __init__(self, poetry: "Poetry") -> None:
2020
"wheel": WheelBuilder,
2121
}
2222

23-
def build(self, fmt: str, executable: Optional[Union[str, Path]] = None) -> None:
23+
def build(self, fmt: str, executable: str | Path | None = None) -> None:
2424
if fmt in self._formats:
2525
builders = [self._formats[fmt]]
2626
elif fmt == "all":

src/poetry/core/masonry/builders/builder.py

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging
24
import re
35
import sys
@@ -6,11 +8,6 @@
68
from collections import defaultdict
79
from pathlib import Path
810
from typing import TYPE_CHECKING
9-
from typing import Dict
10-
from typing import List
11-
from typing import Optional
12-
from typing import Set
13-
from typing import Union
1411

1512

1613
if TYPE_CHECKING:
@@ -30,21 +27,21 @@
3027

3128

3229
class Builder:
33-
format: Optional[str] = None
30+
format: str | None = None
3431

3532
def __init__(
3633
self,
37-
poetry: "Poetry",
34+
poetry: Poetry,
3835
ignore_packages_formats: bool = False,
39-
executable: Optional[Union[Path, str]] = None,
36+
executable: Path | str | None = None,
4037
) -> None:
4138
from poetry.core.masonry.metadata import Metadata
4239
from poetry.core.masonry.utils.module import Module
4340

4441
self._poetry = poetry
4542
self._package = poetry.package
4643
self._path = poetry.file.parent
47-
self._excluded_files: Optional[Set[str]] = None
44+
self._excluded_files: set[str] | None = None
4845
self._executable = Path(executable or sys.executable)
4946

5047
packages = []
@@ -99,7 +96,7 @@ def executable(self) -> Path:
9996
def build(self) -> None:
10097
raise NotImplementedError()
10198

102-
def find_excluded_files(self, fmt: Optional[str] = None) -> Set[str]:
99+
def find_excluded_files(self, fmt: str | None = None) -> set[str]:
103100
if self._excluded_files is None:
104101
from poetry.core.vcs import get_vcs
105102

@@ -140,7 +137,7 @@ def find_excluded_files(self, fmt: Optional[str] = None) -> Set[str]:
140137

141138
return self._excluded_files
142139

143-
def is_excluded(self, filepath: Union[str, Path]) -> bool:
140+
def is_excluded(self, filepath: str | Path) -> bool:
144141
exclude_path = Path(filepath)
145142

146143
while True:
@@ -154,7 +151,7 @@ def is_excluded(self, filepath: Union[str, Path]) -> bool:
154151

155152
return False
156153

157-
def find_files_to_add(self, exclude_build: bool = True) -> Set["BuildIncludeFile"]:
154+
def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]:
158155
"""
159156
Finds all files to add to the tarball
160157
"""
@@ -279,7 +276,7 @@ def get_metadata_content(self) -> str:
279276

280277
return content
281278

282-
def convert_entry_points(self) -> Dict[str, List[str]]:
279+
def convert_entry_points(self) -> dict[str, list[str]]:
283280
result = defaultdict(list)
284281

285282
# Scripts -> Entry points
@@ -320,8 +317,8 @@ def convert_entry_points(self) -> Dict[str, List[str]]:
320317

321318
return dict(result)
322319

323-
def convert_script_files(self) -> List[Path]:
324-
script_files: List[Path] = []
320+
def convert_script_files(self) -> list[Path]:
321+
script_files: list[Path] = []
325322

326323
for name, specification in self._poetry.local_config.get("scripts", {}).items():
327324
if isinstance(specification, dict) and specification.get("type") == "file":
@@ -350,7 +347,7 @@ def convert_script_files(self) -> List[Path]:
350347
return script_files
351348

352349
@classmethod
353-
def convert_author(cls, author: str) -> Dict[str, str]:
350+
def convert_author(cls, author: str) -> dict[str, str]:
354351
m = AUTHOR_REGEX.match(author)
355352

356353
name = m.group("name")
@@ -362,9 +359,9 @@ def convert_author(cls, author: str) -> Dict[str, str]:
362359
class BuildIncludeFile:
363360
def __init__(
364361
self,
365-
path: Union[Path, str],
366-
project_root: Union[Path, str],
367-
source_root: Optional[Union[Path, str]] = None,
362+
path: Path | str,
363+
project_root: Path | str,
364+
source_root: Path | str | None = None,
368365
):
369366
"""
370367
:param project_root: the full path of the project's root
@@ -381,13 +378,13 @@ def __init__(
381378

382379
self.path = self.path.resolve()
383380

384-
def __eq__(self, other: Union["BuildIncludeFile", Path]) -> bool:
381+
def __eq__(self, other: BuildIncludeFile | Path) -> bool:
385382
if hasattr(other, "path"):
386383
return self.path == other.path
387384

388385
return self.path == other
389386

390-
def __ne__(self, other: Union["BuildIncludeFile", Path]) -> bool:
387+
def __ne__(self, other: BuildIncludeFile | Path) -> bool:
391388
return not self.__eq__(other)
392389

393390
def __hash__(self) -> int:

0 commit comments

Comments
 (0)