Skip to content

Commit

Permalink
chore: enable pep 563/585 (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent authored Apr 1, 2022
1 parent 65ddc9e commit d6d33de
Show file tree
Hide file tree
Showing 112 changed files with 709 additions and 643 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ repos:
rev: 5.10.1
hooks:
- id: isort
args: [--add-import, from __future__ import annotations]
exclude: |
(?x)(
^.*/?setup\.py$
Expand Down
2 changes: 2 additions & 0 deletions src/poetry/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import sys

from pathlib import Path
Expand Down
2 changes: 2 additions & 0 deletions src/poetry/core/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from poetry.core.exceptions.base import PoetryCoreException


Expand Down
3 changes: 3 additions & 0 deletions src/poetry/core/exceptions/base.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from __future__ import annotations


class PoetryCoreException(Exception):
pass
41 changes: 21 additions & 20 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging

from pathlib import Path
Expand All @@ -6,7 +8,6 @@
from typing import Dict
from typing import List
from typing import Mapping
from typing import Optional
from typing import Union
from warnings import warn

Expand Down Expand Up @@ -37,8 +38,8 @@ class Factory:
"""

def create_poetry(
self, cwd: Optional[Path] = None, with_groups: bool = True
) -> "Poetry":
self, cwd: Path | None = None, with_groups: bool = True
) -> Poetry:
from poetry.core.poetry import Poetry
from poetry.core.pyproject.toml import PyProjectTOML

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

@classmethod
def get_package(cls, name: str, version: str) -> "ProjectPackage":
def get_package(cls, name: str, version: str) -> ProjectPackage:
from poetry.core.packages.project_package import ProjectPackage

return ProjectPackage(name, version, version)

@classmethod
def _add_package_group_dependencies(
cls,
package: "ProjectPackage",
group: Union[str, "DependencyGroup"],
dependencies: "DependencyConfig",
package: ProjectPackage,
group: str | DependencyGroup,
dependencies: DependencyConfig,
) -> None:
if isinstance(group, str):
if package.has_dependency_group(group):
Expand Down Expand Up @@ -109,11 +110,11 @@ def _add_package_group_dependencies(
@classmethod
def configure_package(
cls,
package: "ProjectPackage",
config: Dict[str, Any],
package: ProjectPackage,
config: dict[str, Any],
root: Path,
with_groups: bool = True,
) -> "ProjectPackage":
) -> ProjectPackage:
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.spdx.helpers import license_by_id
Expand All @@ -131,7 +132,7 @@ def configure_package(
package.repository_url = config.get("repository")
package.documentation_url = config.get("documentation")
try:
license_: Optional["License"] = license_by_id(config.get("license", ""))
license_: License | None = license_by_id(config.get("license", ""))
except ValueError:
license_ = None

Expand Down Expand Up @@ -220,10 +221,10 @@ def configure_package(
def create_dependency(
cls,
name: str,
constraint: "DependencyConstraint",
groups: Optional[List[str]] = None,
root_dir: Optional[Path] = None,
) -> "DependencyTypes":
constraint: DependencyConstraint,
groups: list[str] | None = None,
root_dir: Path | None = None,
) -> DependencyTypes:
from poetry.core.packages.constraints import (
parse_constraint as parse_generic_constraint,
)
Expand Down Expand Up @@ -335,7 +336,7 @@ def create_dependency(
)

if not markers:
marker: "BaseMarker" = AnyMarker()
marker: BaseMarker = AnyMarker()
if python_versions:
marker = marker.intersect(
parse_marker(
Expand Down Expand Up @@ -367,14 +368,14 @@ def create_dependency(

@classmethod
def validate(
cls, config: Dict[str, Any], strict: bool = False
) -> Dict[str, List[str]]:
cls, config: dict[str, Any], strict: bool = False
) -> dict[str, list[str]]:
"""
Checks the validity of a configuration
"""
from poetry.core.json import validate_object

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

Expand Down Expand Up @@ -428,7 +429,7 @@ def validate(
return result

@classmethod
def locate(cls, cwd: Optional[Path] = None) -> Path:
def locate(cls, cwd: Path | None = None) -> Path:
cwd = Path(cwd or Path.cwd())
candidates = [cwd]
candidates.extend(cwd.parents)
Expand Down
6 changes: 3 additions & 3 deletions src/poetry/core/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import json
import os

from typing import Any
from typing import Dict
from typing import List


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


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

if not os.path.exists(schema):
Expand Down
21 changes: 10 additions & 11 deletions src/poetry/core/masonry/api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""
PEP-517 compliant buildsystem API
"""
from __future__ import annotations

import logging

from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import Optional

from poetry.core.factory import Factory
from poetry.core.masonry.builders.sdist import SdistBuilder
Expand All @@ -18,8 +17,8 @@


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


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

def build_wheel(
wheel_directory: str,
config_settings: Optional[Dict[str, Any]] = None,
metadata_directory: Optional[str] = None,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None,
) -> str:
"""Builds a wheel, places it in wheel_directory"""
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)
Expand All @@ -69,7 +68,7 @@ def build_wheel(


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

def build_editable(
wheel_directory: str,
config_settings: Optional[Dict[str, Any]] = None,
metadata_directory: Optional[str] = None,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None,
) -> str:
poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False)

Expand Down
8 changes: 4 additions & 4 deletions src/poetry/core/masonry/builder.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Optional
from typing import Union


if TYPE_CHECKING:
from poetry.core.poetry import Poetry


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

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

def build(self, fmt: str, executable: Optional[Union[str, Path]] = None) -> None:
def build(self, fmt: str, executable: str | Path | None = None) -> None:
if fmt in self._formats:
builders = [self._formats[fmt]]
elif fmt == "all":
Expand Down
39 changes: 18 additions & 21 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import re
import sys
Expand All @@ -6,11 +8,6 @@
from collections import defaultdict
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Union


if TYPE_CHECKING:
Expand All @@ -30,21 +27,21 @@


class Builder:
format: Optional[str] = None
format: str | None = None

def __init__(
self,
poetry: "Poetry",
poetry: Poetry,
ignore_packages_formats: bool = False,
executable: Optional[Union[Path, str]] = None,
executable: Path | str | None = None,
) -> None:
from poetry.core.masonry.metadata import Metadata
from poetry.core.masonry.utils.module import Module

self._poetry = poetry
self._package = poetry.package
self._path = poetry.file.parent
self._excluded_files: Optional[Set[str]] = None
self._excluded_files: set[str] | None = None
self._executable = Path(executable or sys.executable)

packages = []
Expand Down Expand Up @@ -99,7 +96,7 @@ def executable(self) -> Path:
def build(self) -> None:
raise NotImplementedError()

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

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

return self._excluded_files

def is_excluded(self, filepath: Union[str, Path]) -> bool:
def is_excluded(self, filepath: str | Path) -> bool:
exclude_path = Path(filepath)

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

return False

def find_files_to_add(self, exclude_build: bool = True) -> Set["BuildIncludeFile"]:
def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]:
"""
Finds all files to add to the tarball
"""
Expand Down Expand Up @@ -279,7 +276,7 @@ def get_metadata_content(self) -> str:

return content

def convert_entry_points(self) -> Dict[str, List[str]]:
def convert_entry_points(self) -> dict[str, list[str]]:
result = defaultdict(list)

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

return dict(result)

def convert_script_files(self) -> List[Path]:
script_files: List[Path] = []
def convert_script_files(self) -> list[Path]:
script_files: list[Path] = []

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

@classmethod
def convert_author(cls, author: str) -> Dict[str, str]:
def convert_author(cls, author: str) -> dict[str, str]:
m = AUTHOR_REGEX.match(author)

name = m.group("name")
Expand All @@ -362,9 +359,9 @@ def convert_author(cls, author: str) -> Dict[str, str]:
class BuildIncludeFile:
def __init__(
self,
path: Union[Path, str],
project_root: Union[Path, str],
source_root: Optional[Union[Path, str]] = None,
path: Path | str,
project_root: Path | str,
source_root: Path | str | None = None,
):
"""
:param project_root: the full path of the project's root
Expand All @@ -381,13 +378,13 @@ def __init__(

self.path = self.path.resolve()

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

return self.path == other

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

def __hash__(self) -> int:
Expand Down
Loading

0 comments on commit d6d33de

Please sign in to comment.