Skip to content

Commit

Permalink
add mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbit72 committed Apr 9, 2021
1 parent 42369f8 commit dd5e1b5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
script: isort . --check
- name: flake8
script: flake8 .
- name: mypy
script: mypy .
- stage: test
python: 3.6
- python: 3.7
Expand Down
5 changes: 5 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[mypy]
python_version = 3.6

[mypy-tests.*]
ignore_errors = True
90 changes: 65 additions & 25 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ isort = {version = "*", extras = ["pyproject"]}
flake8 = "*"
flake8-bugbear = "*"
pre-commit = "*"
mypy = "^0.812"


[tool.poetry.dependencies]
python = "^3.6"
importlib_metadata = { version = "^3.0", python = "< 3.8" }


[tool.black]
line-length = 88
target-version = ['py36']

# backward compatibility with black
[tool.isort]
multi_line_output = 3
Expand Down
Empty file added single_source/py.typed
Empty file.
13 changes: 7 additions & 6 deletions single_source/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
# for Python <3.8 add 'importlib_metadata' as a dependency
import importlib_metadata
import importlib_metadata # type: ignore


VERSION_REGEX = r"\s*version\s*=\s*[\"']\s*([-.\w]{3,})\s*[\"']\s*"
Expand Down Expand Up @@ -58,7 +58,7 @@ def get_version(

version: Optional[str] = _get_version_from_metadata(package_name)
if version is None:
version: Optional[str] = _get_version_from_path(target_path, version_regex)
version = _get_version_from_path(target_path, version_regex)

if not version and fail:
raise VersionNotFoundError(
Expand All @@ -71,8 +71,8 @@ def get_version(
def _get_version_from_metadata(package_name: str) -> Optional[str]:
"""Implements a getting version flow for installed package"""
try:
version = importlib_metadata.version(package_name)
except importlib_metadata.PackageNotFoundError:
version: str = importlib_metadata.version(package_name) # type: ignore
except importlib_metadata.PackageNotFoundError: # type: ignore
return None
else:
return version.strip()
Expand All @@ -87,8 +87,9 @@ def _get_version_from_path(file_path: Path, version_regex: str) -> Optional[str]
try:
with file_path.open(mode="r", encoding="utf-8") as file_with_version:
for line in file_with_version:
version: re.Match = compiled_version_regex.search(line)
version: Optional[re.Match] = compiled_version_regex.search(line) # type: ignore # noqa: E501
if version is not None:
return version.group(1).strip()
except (FileNotFoundError, UnicodeDecodeError):
return None
pass
return None
2 changes: 1 addition & 1 deletion tests/data/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import find_packages, setup
from setuptools import find_packages, setup # type: ignore

setup(
name="HelloWorld",
Expand Down

0 comments on commit dd5e1b5

Please sign in to comment.