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

Added mypy POC and attempted to fix issues #1114

Merged
merged 4 commits into from
Aug 2, 2021
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ docs/docblocks/
*egg-info/
*eggs/
*.cfg
!mypy.cfg
*.sqlite
*~
*.swp
Expand Down
31 changes: 13 additions & 18 deletions .zuul.yaml
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
- job:
name: tox-lint
name: tox-mypy
description: |
Run lint tests using tox.
Run static-linker tests with mypy.

Uses tox with ``lint`` environment.
Uses tox with ``mypy`` environment.

parent: tox
run: ci/test-tox.yaml
vars:
tox_env: lint
tox_env: mypy
nodeset:
nodes:
name: test-node
label: pod-python-f33

- job:
name: tox-format
name: tox-lint
description: |
Run format tests using tox.
Run lint tests using tox.

Uses tox with ``format`` environment.
Uses tox with ``lint`` environment.

parent: tox
run: ci/test-tox.yaml
vars:
tox_env: format
tox_env: lint
nodeset:
nodes:
name: test-node
label: pod-python-f33

- job:
name: tox-python37
name: tox-format
description: |
Run unit tests for a Python project under cPython version 3.7.

Uses tox with the ``py37`` environment.
Run format tests using tox.

Ensures that the python37 interpreter is installed.
Uses tox with ``format`` environment.

parent: tox
run: ci/test-tox.yaml
vars:
tox_env: py37
dependencies:
- python37
tox_env: format
nodeset:
nodes:
name: test-node
Expand Down Expand Up @@ -148,9 +143,9 @@
- project:
check:
jobs:
- tox-mypy
- tox-lint
- tox-format
- tox-python37
- tox-python38
- tox-python39
- anitya-tox-docs
Expand Down
2 changes: 1 addition & 1 deletion anitya/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

# Start with a basic logging configuration, which will be replaced by any user-
# specified logging configuration when the configuration is loaded.
logging.config.dictConfig(DEFAULTS["ANITYA_LOG_CONFIG"])
logging.config.dictConfig(DEFAULTS["ANITYA_LOG_CONFIG"]) # type: ignore


def load():
Expand Down
10 changes: 1 addition & 9 deletions anitya/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""SQLAlchemy database models."""

try:
# The Python 3.6+ API
from secrets import choice as random_choice
except ImportError: # pragma: no cover
# Fall back to random with os.urandom
import random

random = random.SystemRandom()
random_choice = random.choice
from secrets import choice as random_choice
import datetime
import arrow
import logging
Expand Down
8 changes: 2 additions & 6 deletions anitya/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ class TokenForm(FlaskForm):
class ProjectForm(FlaskForm):
name = StringField("Project name", [validators.DataRequired()])
homepage = StringField("Homepage", [validators.DataRequired(), validators.URL()])
backend = SelectField(
"Backend", [validators.DataRequired()], choices=[(item, item) for item in []]
)
backend = SelectField("Backend", [validators.DataRequired()], choices=[])
version_url = StringField("Version URL", [validators.optional()])
version_prefix = StringField("Version prefix", [validators.optional()])
pre_release_filter = StringField("Pre-release filter", [validators.optional()])
version_filter = StringField("Version filter", [validators.optional()])
version_scheme = SelectField(
"Version scheme", [validators.Required()], choices=[(item, item) for item in []]
)
version_scheme = SelectField("Version scheme", [validators.Required()], choices=[])
version_pattern = StringField("Version pattern", [validators.optional()])
regex = StringField("Regex", [validators.optional()])
insecure = BooleanField("Use insecure connection", [validators.optional()])
Expand Down
14 changes: 8 additions & 6 deletions anitya/lib/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

# sre_constants contains re exceptions
import sre_constants
from typing import List
import urllib.request as urllib
from urllib.error import URLError

Expand All @@ -35,7 +36,8 @@

from anitya.config import config as anitya_config
from anitya.lib.exceptions import AnityaPluginException
from anitya.lib.versions import RpmVersion
from anitya.lib.versions import GLOBAL_DEFAULT, RpmVersion

import six

REGEX = anitya_config["DEFAULT_REGEX"]
Expand Down Expand Up @@ -79,11 +81,11 @@ class BaseBackend(object):
checking for new versions. This could be overriden by backend plugin.
"""

name = None
examples = None
default_regex = None
more_info = None
default_version_scheme = None
name: str
examples: List[str]
default_regex: str
more_info: str
default_version_scheme = GLOBAL_DEFAULT
check_interval = timedelta(hours=1)

@classmethod
Expand Down
12 changes: 8 additions & 4 deletions anitya/lib/ecosystems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"""


from typing import List
from anitya.lib.versions import GLOBAL_DEFAULT as DEFAULT_VERSION_SCHEME


class BaseEcosystem(object):
"""
The base class that all the different ecosystems should extend.
Expand All @@ -28,7 +32,7 @@ class BaseEcosystem(object):
should be lowercase.
"""

name = None
default_backend = None
default_version_scheme = None
aliases = []
name: str
default_backend: str
default_version_scheme: str = DEFAULT_VERSION_SCHEME
aliases: List[str] = []
3 changes: 1 addition & 2 deletions anitya/lib/versions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ def __init__(
else:
self.prefixes = []
self.created_on = created_on
self.pattern = None
if pattern:
self.pattern = pattern.upper()
else:
self.pattern = None
self.cursor = cursor
self.commit_url = commit_url
if pre_release_filter:
Expand Down
77 changes: 53 additions & 24 deletions anitya/lib/versions/calver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""
import functools
import re
from typing import Tuple
from typing import Optional, Tuple, TypedDict

from .base import Version

Expand All @@ -53,6 +53,16 @@ def split_by_match(regex: str, string: str) -> Tuple[str, str]:
return "", string


class SplitResult(TypedDict, total=True):
Copy link
Contributor

@Zlopez Zlopez Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of having multiple classes in one .py file, but this looks good. I didn't knew about the TypedDict till now.

year: Optional[str]
month: Optional[str]
day: Optional[str]
minor: Optional[str]
micro: Optional[str]
modifier: Optional[str]
rc_number: Optional[str]


@functools.total_ordering
class CalendarVersion(Version):
"""
Expand All @@ -70,7 +80,7 @@ class CalendarVersion(Version):

name = "Calendar"

def split(self) -> dict:
def split(self) -> SplitResult:
"""
Does a simple lexical analysis of the version against pattern.

Expand All @@ -82,14 +92,15 @@ def split(self) -> dict:
This could happen if the pattern is not correct or if
the version can't be parsed by the pattern.
"""
result_dict = {
"year": None,
"month": None,

result_dict: SplitResult = {
"day": None,
"minor": None,
"micro": None,
"minor": None,
"modifier": None,
"month": None,
"rc_number": None,
"year": None,
}
# Check if the pattern is correct
if not self.pattern:
Expand All @@ -113,29 +124,32 @@ def split(self) -> dict:
elif pattern_str.startswith("YY"):
match = re.match(r"[1-9]\d{1,2}", version_str)
if match:
result_dict["year"] = match.group(0)
year = match.group(0)
else:
raise ValueError("Can't parse version by pattern")
index_version = len(result_dict["year"])
result_dict["year"] = year
index_version = len(year)
index_pattern = 2
elif pattern_str.startswith("0Y"):
match = re.match(r"[1-9]\d{1,2}|(0\d)", version_str)
if match:
result_dict["year"] = match.group(0)
year = match.group(0)
else:
raise ValueError("Can't parse version by pattern")
index_version = len(result_dict["year"])
result_dict["year"] = year
index_version = len(year)
index_pattern = 2
elif pattern_str.startswith("MM"):
try:
match = re.match(r"(1\d)|\d", version_str)
if match and 1 <= int(match.group(0)) <= 12:
result_dict["month"] = match.group(0)
month = match.group(0)
else:
raise ValueError("Can't parse version by pattern")
except ValueError:
raise ValueError("Can't parse version by pattern")
index_version = len(result_dict["month"])
result_dict["month"] = month
index_version = len(month)
index_pattern = 2
elif pattern_str.startswith("0M"):
try:
Expand All @@ -156,12 +170,13 @@ def split(self) -> dict:
try:
match = re.match(r"([1-3]\d)|\d", version_str)
if match and 1 <= int(match.group(0)) <= 31:
result_dict["day"] = match.group(0)
day = match.group(0)
else:
raise ValueError("Can't parse version by pattern")
except ValueError:
raise ValueError("Can't parse version by pattern")
index_version = len(result_dict["day"])
result_dict["day"] = day
index_version = len(day)
index_pattern = 2
elif pattern_str.startswith("0D"):
try:
Expand Down Expand Up @@ -258,12 +273,16 @@ def prerelease(self) -> bool:
return True

for pre_release_filter in self.pre_release_filters:
if pre_release_filter and pre_release_filter in self.version:
if (
pre_release_filter
and self.version
and pre_release_filter in self.version
):
return True

return False

def __eq__(self, other: Version) -> bool:
def __eq__(self, other) -> bool:
"""
Compare two versions for equality using the calendar rules with pre-release
support.
Expand All @@ -274,9 +293,11 @@ def __eq__(self, other: Version) -> bool:
Returns:
(bool) True if equal, False otherwise.
"""
if not isinstance(other, Version):
return NotImplemented
try:
version_dict_self = self.split()
version_dict_other = other.split()
version_dict_self = self.split() # type: ignore
version_dict_other = other.split() # type: ignore
except ValueError:
# The version can't be split, so compare them as strings
if self.parse() == other.parse():
Expand All @@ -289,6 +310,12 @@ def __eq__(self, other: Version) -> bool:

return False

def maybe_split(self) -> Optional[SplitResult]:
try:
return self.split()
except ValueError:
return None

def __lt__(self, other: Version) -> bool:
"""
Compare two versions for lower than using the calendar rules with pre-release
Expand All @@ -300,15 +327,17 @@ def __lt__(self, other: Version) -> bool:
Returns:
(bool) True if lower than, False otherwise.
"""
try:
version_dict_self = self.split()
except ValueError:
version_dict_self = None
if isinstance(self, CalendarVersion):
version_dict_self = self.maybe_split()
if not version_dict_self:
# The version can't be split, so we consider it lesser
return True

try:
version_dict_other = other.split()
except ValueError:
version_dict_other = None
if isinstance(other, CalendarVersion):
version_dict_other = other.maybe_split()
if not version_dict_other:
# The version can't be split, so we consider self greater
return False

Expand Down
5 changes: 1 addition & 4 deletions anitya/lib/versions/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@

warnings.warn("Failed to import 'rpm', emulating RPM label comparisons")

try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
from itertools import zip_longest

_subfield_pattern = re.compile(
r"(?P<junk>[^a-zA-Z0-9]*)((?P<text>[a-zA-Z]+)|(?P<num>[0-9]+))"
Expand Down
4 changes: 3 additions & 1 deletion anitya/mail_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

psutil = None
try:
import psutil
import psutil as _psutil

psutil = _psutil # pragma: no cover
except (OSError, ImportError): # pragma: no cover
# We run into issues when trying to import psutil from inside mod_wsgi on
# rhel7. If we hit that here, then just fail quietly.
Expand Down
Loading