Skip to content

Commit

Permalink
Translate the rest of the type comments
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Sep 7, 2024
1 parent 29dd2e6 commit 0efb541
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ select = [
]
ignore = [
"E501",
"F401", # Ruff doesn't seem to understand type comments?
"UP031", # TODO: Avoid % formatting.
]

Expand Down
88 changes: 43 additions & 45 deletions src/incremental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import sys
import warnings
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, BinaryIO, Dict, Optional, TypeVar, Union
from typing import TYPE_CHECKING, Any, BinaryIO, Dict, Optional, Union

if TYPE_CHECKING:
import io
from distutils.dist import Distribution as _Distribution

from typing_extensions import Literal
Expand All @@ -25,7 +24,7 @@
#


def _cmp(a, b): # type: (Any, Any) -> int
def _cmp(a: Any, b: Any) -> int:
"""
Compare two objects.
Expand All @@ -50,28 +49,27 @@ class _Inf:
An object that is bigger than all other objects.
"""

def __cmp__(self, other): # type: (object) -> int
def __cmp__(self, other: object) -> int:
"""
@param other: Another object.
@type other: any
@return: 0 if other is inf, 1 otherwise.
@rtype: C{int}
"""
if other is _inf:
return 0
return 1

def __lt__(self, other): # type: (object) -> bool
def __lt__(self, other: object) -> bool:
return self.__cmp__(other) < 0

def __le__(self, other): # type: (object) -> bool
def __le__(self, other: object) -> bool:
return self.__cmp__(other) <= 0

def __gt__(self, other): # type: (object) -> bool
def __gt__(self, other: object) -> bool:
return self.__cmp__(other) > 0

def __ge__(self, other): # type: (object) -> bool
def __ge__(self, other: object) -> bool:
return self.__cmp__(other) >= 0


Expand All @@ -95,14 +93,14 @@ class Version:

def __init__(
self,
package, # type: str
major, # type: Union[Literal["NEXT"], int]
minor, # type: int
micro, # type: int
release_candidate=None, # type: Optional[int]
prerelease=None, # type: Optional[int]
post=None, # type: Optional[int]
dev=None, # type: Optional[int]
package: str,
major: Union[Literal["NEXT"], int],
minor: int,
micro: int,
release_candidate: Optional[int] = None,
prerelease: Optional[int] = None,
post: Optional[int] = None,
dev: Optional[int] = None,
):
"""
@param package: Name of the package that this is a version of.
Expand Down Expand Up @@ -149,7 +147,7 @@ def __init__(
self.dev = dev

@property
def prerelease(self): # type: () -> Optional[int]
def prerelease(self) -> Optional[int]:
warnings.warn(
"Accessing incremental.Version.prerelease was "
"deprecated in Incremental 16.9.0. Use "
Expand All @@ -159,7 +157,7 @@ def prerelease(self): # type: () -> Optional[int]
)
return self.release_candidate

def public(self): # type: () -> str
def public(self) -> str:
"""
Return a PEP440-compatible "public" representation of this L{Version}.
Expand Down Expand Up @@ -194,7 +192,7 @@ def public(self): # type: () -> str
short = public
local = public

def __repr__(self): # type: () -> str
def __repr__(self) -> str:
if self.release_candidate is None:
release_candidate = ""
else:
Expand All @@ -221,10 +219,10 @@ def __repr__(self): # type: () -> str
dev,
)

def __str__(self): # type: () -> str
def __str__(self) -> str:
return "[%s, version %s]" % (self.package, self.short())

def __cmp__(self, other): # type: (object) -> int
def __cmp__(self, other: object) -> int:
"""
Compare two versions, considering major versions, minor versions, micro
versions, then release candidates, then postreleases, then dev
Expand Down Expand Up @@ -253,12 +251,12 @@ def __cmp__(self, other): # type: (object) -> int
raise IncomparableVersions("%r != %r" % (self.package, other.package))

if self.major == "NEXT":
major = _inf # type: Union[int, _Inf]
major: Union[int, _Inf] = _inf
else:
major = self.major

if self.release_candidate is None:
release_candidate = _inf # type: Union[int, _Inf]
release_candidate: Union[int, _Inf] = _inf
else:
release_candidate = self.release_candidate

Expand All @@ -268,17 +266,17 @@ def __cmp__(self, other): # type: (object) -> int
post = self.post

if self.dev is None:
dev = _inf # type: Union[int, _Inf]
dev: Union[int, _Inf] = _inf
else:
dev = self.dev

if other.major == "NEXT":
othermajor = _inf # type: Union[int, _Inf]
othermajor: Union[int, _Inf] = _inf
else:
othermajor = other.major

if other.release_candidate is None:
otherrc = _inf # type: Union[int, _Inf]
otherrc: Union[int, _Inf] = _inf
else:
otherrc = other.release_candidate

Expand All @@ -288,7 +286,7 @@ def __cmp__(self, other): # type: (object) -> int
otherpost = other.post

if other.dev is None:
otherdev = _inf # type: Union[int, _Inf]
otherdev: Union[int, _Inf] = _inf
else:
otherdev = other.dev

Expand All @@ -298,44 +296,44 @@ def __cmp__(self, other): # type: (object) -> int
)
return x

def __eq__(self, other): # type: (object) -> bool
def __eq__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c == 0

def __ne__(self, other): # type: (object) -> bool
def __ne__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c != 0

def __lt__(self, other): # type: (object) -> bool
def __lt__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c < 0

def __le__(self, other): # type: (object) -> bool
def __le__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c <= 0

def __gt__(self, other): # type: (object) -> bool
def __gt__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c > 0

def __ge__(self, other): # type: (object) -> bool
def __ge__(self, other: object) -> bool:
c = self.__cmp__(other)
if c is NotImplemented:
return c # type: ignore[return-value]
return c >= 0


def getVersionString(version): # type: (Version) -> str
def getVersionString(version: Version) -> str:
"""
Get a friendly string for the given version object.
Expand All @@ -346,7 +344,7 @@ def getVersionString(version): # type: (Version) -> str
return result


def _findPath(path, package): # type: (str, str) -> str
def _findPath(path: str, package: str) -> str:
"""
Determine the package root directory.
Expand All @@ -370,19 +368,19 @@ def _findPath(path, package): # type: (str, str) -> str
)


def _existing_version(version_path): # type: (str) -> Version
def _existing_version(version_path: str) -> Version:
"""
Load the current version from a ``_version.py`` file.
"""
version_info = {} # type: Dict[str, Version]
version_info: Dict[str, Version] = {}

with open(version_path) as f:
exec(f.read(), version_info)

return version_info["__version__"]


def _get_setuptools_version(dist): # type: (_Distribution) -> None
def _get_setuptools_version(dist: _Distribution) -> None:
"""
Setuptools integration: load the version from the working directory
Expand Down Expand Up @@ -417,7 +415,7 @@ def _get_setuptools_version(dist): # type: (_Distribution) -> None
dist.metadata.version = version.public()


def _get_distutils_version(dist, keyword, value): # type: (_Distribution, object, object) -> None
def _get_distutils_version(dist: _Distribution, keyword: object, value: object) -> None:
"""
Distutils integration: get the version from the package listed in the Distribution.
Expand All @@ -442,7 +440,7 @@ def _get_distutils_version(dist, keyword, value): # type: (_Distribution, objec
raise Exception("No _version.py found.") # pragma: no cover


def _load_toml(f): # type: (BinaryIO) -> Any
def _load_toml(f: BinaryIO) -> Any:
"""
Read the content of a TOML file.
"""
Expand Down Expand Up @@ -476,12 +474,12 @@ class _IncrementalConfig:
"""Path to the package root"""

@property
def version_path(self): # type: () -> str
def version_path(self) -> str:
"""Path of the ``_version.py`` file. May not exist."""
return os.path.join(self.path, "_version.py")


def _load_pyproject_toml(toml_path): # type: (str) -> _IncrementalConfig
def _load_pyproject_toml(toml_path: str) -> _IncrementalConfig:
"""
Load Incremental configuration from a ``pyproject.toml``
Expand Down Expand Up @@ -532,7 +530,7 @@ def _load_pyproject_toml(toml_path): # type: (str) -> _IncrementalConfig
)


def _extract_tool_incremental(data): # type: (Dict[str, object]) -> Optional[Dict[str, object]]
def _extract_tool_incremental(data: Dict[str, object]) -> Optional[Dict[str, object]]:
if "tool" not in data:
return None
if not isinstance(data["tool"], dict):
Expand All @@ -552,7 +550,7 @@ def _extract_tool_incremental(data): # type: (Dict[str, object]) -> Optional[Di
from ._version import __version__ # noqa: E402


def _setuptools_version(): # type: () -> str
def _setuptools_version() -> str:
return __version__.public() # pragma: no cover


Expand Down
2 changes: 1 addition & 1 deletion src/incremental/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import datetime
import os
from argparse import ArgumentParser
from typing import Any, Callable, Dict, Optional, Sequence
from typing import Any, Callable, Optional, Sequence

from incremental import Version, _existing_version, _findPath

Expand Down
4 changes: 2 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
from subprocess import run
from tempfile import TemporaryDirectory

from incremental import Version
from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase

from build import BuildBackendException, ProjectBuilder
from build.env import DefaultIsolatedEnv
from incremental import Version

TEST_DIR = FilePath(os.path.abspath(os.path.dirname(__file__)))


def build_and_install(path): # type: (FilePath) -> None
def build_and_install(path: FilePath) -> None:
with TemporaryDirectory(prefix="dist") as dist_dir:
with DefaultIsolatedEnv(installer="pip") as env:
env.install(
Expand Down

0 comments on commit 0efb541

Please sign in to comment.