Skip to content

Commit

Permalink
Merge pull request #52 from sbrunner/types
Browse files Browse the repository at this point in the history
Add the types
  • Loading branch information
carlio authored Nov 8, 2024
2 parents 6921ee3 + 2e05560 commit ae17781
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 71 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ homepage = "https://github.com/landscapeio/requirements-detector"
packages = [
{ include = "requirements_detector/"}
]
include = [
"c2cgeoform/py.typed",
]

[tool.poetry.scripts]
detect-requirements = 'requirements_detector.run:run'
Expand Down
4 changes: 2 additions & 2 deletions requirements_detector/poetry_semver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
__version__ = "0.1.0"


def parse_constraint(constraints): # type: (str) -> VersionConstraint
def parse_constraint(constraints: str) -> VersionConstraint:
if constraints == "*":
return VersionRange()

Expand Down Expand Up @@ -47,7 +47,7 @@ def parse_constraint(constraints): # type: (str) -> VersionConstraint
return VersionUnion.of(*or_groups)


def parse_single_constraint(constraint): # type: (str) -> VersionConstraint
def parse_single_constraint(constraint: str) -> VersionConstraint:
m = re.match(r"(?i)^v?[xX*](\.[xX*])*$", constraint)
if m:
return VersionRange()
Expand Down
76 changes: 38 additions & 38 deletions requirements_detector/poetry_semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Version(VersionRange):

def __init__(
self,
major, # type: int
minor=None, # type: Optional[int]
patch=None, # type: Optional[int]
rest=None, # type: Optional[int]
pre=None, # type: Optional[str]
build=None, # type: Optional[str]
text=None, # type: Optional[str]
precision=None, # type: Optional[int]
): # type: (...) -> None
major: int,
minor: Optional[int] = None,
patch: Optional[int] = None,
rest: Optional[int] = None,
pre: Optional[str] = None,
build: Optional[str] = None,
text: Optional[str] = None,
precision: Optional[int] = None,
) -> None:
self._major = int(major)
self._precision = None
if precision is None:
Expand Down Expand Up @@ -92,35 +92,35 @@ def __init__(
self._build = self._split_parts(build)

@property
def major(self): # type: () -> int
def major(self) -> int:
return self._major

@property
def minor(self): # type: () -> int
def minor(self) -> int:
return self._minor

@property
def patch(self): # type: () -> int
def patch(self) -> int:
return self._patch

@property
def rest(self): # type: () -> int
def rest(self) -> int:
return self._rest

@property
def prerelease(self): # type: () -> List[str]
def prerelease(self) -> List[str]:
return self._prerelease

@property
def build(self): # type: () -> List[str]
def build(self) -> List[str]:
return self._build

@property
def text(self):
return self._text

@property
def precision(self): # type: () -> int
def precision(self) -> int:
return self._precision

@property
Expand All @@ -131,28 +131,28 @@ def stable(self):
return self.next_patch

@property
def next_major(self): # type: () -> Version
def next_major(self) -> "Version":
if self.is_prerelease() and self.minor == 0 and self.patch == 0:
return Version(self.major, self.minor, self.patch)

return self._increment_major()

@property
def next_minor(self): # type: () -> Version
def next_minor(self) -> "Version":
if self.is_prerelease() and self.patch == 0:
return Version(self.major, self.minor, self.patch)

return self._increment_minor()

@property
def next_patch(self): # type: () -> Version
def next_patch(self) -> "Version":
if self.is_prerelease():
return Version(self.major, self.minor, self.patch)

return self._increment_patch()

@property
def next_breaking(self): # type: () -> Version
def next_breaking(self) -> "Version":
if self.major == 0:
if self.minor != 0:
return self._increment_minor()
Expand All @@ -167,7 +167,7 @@ def next_breaking(self): # type: () -> Version
return self._increment_major()

@property
def first_prerelease(self): # type: () -> Version
def first_prerelease(self) -> "Version":
return Version.parse("{}.{}.{}-alpha.0".format(self.major, self.minor, self.patch))

@property
Expand All @@ -191,7 +191,7 @@ def include_max(self):
return True

@classmethod
def parse(cls, text): # type: (str) -> Version
def parse(cls, text: str) -> "Version":
try:
match = COMPLETE_VERSION.match(text)
except TypeError:
Expand Down Expand Up @@ -221,25 +221,25 @@ def is_any(self):
def is_empty(self):
return False

def is_prerelease(self): # type: () -> bool
def is_prerelease(self) -> bool:
return len(self._prerelease) > 0

def allows(self, version): # type: (Version) -> bool
def allows(self, version: "Version") -> bool:
return self == version

def allows_all(self, other): # type: (VersionConstraint) -> bool
def allows_all(self, other: VersionConstraint) -> bool:
return other.is_empty() or other == self

def allows_any(self, other): # type: (VersionConstraint) -> bool
def allows_any(self, other: VersionConstraint) -> bool:
return other.allows(self)

def intersect(self, other): # type: (VersionConstraint) -> VersionConstraint
def intersect(self, other: VersionConstraint) -> VersionConstraint:
if other.allows(self):
return self

return EmptyConstraint()

def union(self, other): # type: (VersionConstraint) -> VersionConstraint
def union(self, other: VersionConstraint) -> VersionConstraint:
from .version_range import VersionRange

if other.allows(self):
Expand All @@ -264,25 +264,25 @@ def union(self, other): # type: (VersionConstraint) -> VersionConstraint

return VersionUnion.of(self, other)

def difference(self, other): # type: (VersionConstraint) -> VersionConstraint
def difference(self, other: VersionConstraint) -> VersionConstraint:
if other.allows(self):
return EmptyConstraint()

return self

def equals_without_prerelease(self, other): # type: (Version) -> bool
def equals_without_prerelease(self, other: "Version") -> bool:
return self.major == other.major and self.minor == other.minor and self.patch == other.patch

def _increment_major(self): # type: () -> Version
def _increment_major(self) -> "Version":
return Version(self.major + 1, 0, 0, precision=self._precision)

def _increment_minor(self): # type: () -> Version
def _increment_minor(self) -> "Version":
return Version(self.major, self.minor + 1, 0, precision=self._precision)

def _increment_patch(self): # type: () -> Version
def _increment_patch(self) -> "Version":
return Version(self.major, self.minor, self.patch + 1, precision=self._precision)

def _normalize_prerelease(self, pre): # type: (str) -> str
def _normalize_prerelease(self, pre: str) -> str:
if not pre:
return

Expand All @@ -307,7 +307,7 @@ def _normalize_prerelease(self, pre): # type: (str) -> str

return "{}.{}".format(modifier, number)

def _normalize_build(self, build): # type: (str) -> str
def _normalize_build(self, build: str) -> str:
if not build:
return

Expand All @@ -319,7 +319,7 @@ def _normalize_build(self, build): # type: (str) -> str

return build

def _split_parts(self, text): # type: (str) -> List[Union[str, int]]
def _split_parts(self, text: str) -> List[Union[str, int]]:
parts = text.split(".")

for i, part in enumerate(parts):
Expand Down Expand Up @@ -389,7 +389,7 @@ def _cmp_parts(self, a, b):

return 0

def _cmp_lists(self, a, b): # type: (List, List) -> int
def _cmp_lists(self, a: List, b: List) -> int:
for i in range(max(len(a), len(b))):
a_part = None
if i < len(a):
Expand Down Expand Up @@ -422,7 +422,7 @@ def _cmp_lists(self, a, b): # type: (List, List) -> int

return 0

def __eq__(self, other): # type: (Version) -> bool
def __eq__(self, other: "Version") -> bool:
if not isinstance(other, Version):
return NotImplemented

Expand Down
16 changes: 8 additions & 8 deletions requirements_detector/poetry_semver/version_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@


class VersionConstraint:
def is_empty(self): # type: () -> bool
def is_empty(self) -> bool:
raise NotImplementedError()

def is_any(self): # type: () -> bool
def is_any(self) -> bool:
raise NotImplementedError()

def allows(self, version): # type: (semver.Version) -> bool
def allows(self, version: semver.Version) -> bool:
raise NotImplementedError()

def allows_all(self, other): # type: (VersionConstraint) -> bool
def allows_all(self, other: "VersionConstraint") -> bool:
raise NotImplementedError()

def allows_any(self, other): # type: (VersionConstraint) -> bool
def allows_any(self, other: "VersionConstraint") -> bool:
raise NotImplementedError()

def intersect(self, other): # type: (VersionConstraint) -> VersionConstraint
def intersect(self, other: "VersionConstraint") -> "VersionConstraint":
raise NotImplementedError()

def union(self, other): # type: (VersionConstraint) -> VersionConstraint
def union(self, other: "VersionConstraint") -> "VersionConstraint":
raise NotImplementedError()

def difference(self, other): # type: (VersionConstraint) -> VersionConstraint
def difference(self, other: "VersionConstraint") -> "VersionConstraint":
raise NotImplementedError()
Loading

0 comments on commit ae17781

Please sign in to comment.