Skip to content

Commit

Permalink
style: Enforce mypy no-implicit-optional setting (#188)
Browse files Browse the repository at this point in the history
Which has been introduced in `mypy==0.981` and enforce proper type
annotations for args / kwargs with default `None` value.
  • Loading branch information
playpauseandstop authored Oct 23, 2022
1 parent d96a47f commit 5465292
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
18 changes: 10 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line_length = 79
target_version = ["py37"]
Expand All @@ -10,8 +14,9 @@ source = ["badabump"]
source = ["./src/"]

[tool.coverage.report]
exclude_lines = ["if TYPE_CHECKING:", "@overload"]
omit = ["./src/*/__main__.py", "./src/*/annotations.py"]
fail_under = 95
omit = ["./src/badabump/__main__.py", "./src/badabump/ci/__main__.py"]
skip_covered = true
show_missing = true

Expand All @@ -32,12 +37,13 @@ disallow_incomplete_defs = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
exclude = ["docs/", "tests/"]
exclude = ["./docs/", "./migrations/", "./tests/"]
follow_imports = "normal"
follow_imports_for_stubs = true
ignore_missing_imports = false
mypy_path = "./src/"
namespace_packages = true
no_implicit_optional = true
python_executable = "./.venv/bin/python3"
show_column_numbers = true
show_error_codes = true
Expand Down Expand Up @@ -68,7 +74,7 @@ packages = [
]
keywords = ["changelog", "conventional commit", "bump", "version", "calver", "semver", "pre release"]
classifiers = [
"Development Status :: 4 - Beta",
"Development Status :: 5 - Stable",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Environment :: Console",
Expand Down Expand Up @@ -104,7 +110,7 @@ badabump-ci = "badabump.cli.ci_app:main"
"Bug Tracker" = "https://github.com/playpauseandstop/badabump/issues"

[tool.pytest.ini_options]
minversion = "7.1.2"
minversion = "7.1.3"
testpaths = ["./tests/"]
addopts = "--cov --no-cov-on-fail"
log_level = "info"
Expand Down Expand Up @@ -141,7 +147,3 @@ commands_pre =
poetry install
poetry run python3 -m pip install attrs==22.1.0 tomli==1.2.0
"""

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
4 changes: 2 additions & 2 deletions src/badabump/cli/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
import os
import sys
from typing import Optional
from typing import Optional, Union

from badabump import __app__, __version__
from badabump.annotations import Argv
Expand Down Expand Up @@ -82,7 +82,7 @@ def parse_args(argv: Argv) -> argparse.Namespace:
return parser.parse_args(argv)


def main(argv: Argv = None) -> int:
def main(argv: Union[Argv, None] = None) -> int:
# Parse arguments
args = parse_args(argv or sys.argv[1:])

Expand Down
4 changes: 2 additions & 2 deletions src/badabump/cli/ci_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
import sys
from typing import cast
from typing import cast, Union

from badabump import __app__, __version__
from badabump.annotations import Argv
Expand Down Expand Up @@ -99,7 +99,7 @@ def prepare_tag(args: argparse.Namespace, *, config: ProjectConfig) -> int:
return 0


def main(argv: Argv = None) -> int:
def main(argv: Union[Argv, None] = None) -> int:
args = parse_args(argv or sys.argv[1:])
# TODO: Fix this by providing required flag on adding subparsers
if getattr(args, "func", None) is None:
Expand Down
7 changes: 6 additions & 1 deletion src/badabump/cli/output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from difflib import ndiff
from typing import Union


EMPTY = "-"
Expand All @@ -19,7 +20,11 @@ def echo_message(message: str, *, is_dry_run: bool) -> None:


def echo_value(
label: str, value: str, *, is_ci: bool = False, ci_name: str = None
label: str,
value: str,
*,
is_ci: bool = False,
ci_name: Union[str, None] = None,
) -> None:
if is_ci and ci_name:
github_actions_output(ci_name, value)
Expand Down
30 changes: 21 additions & 9 deletions src/badabump/versions/semver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Type, TypeVar, Union

import attr

from badabump.annotations import DictStrStr
Expand All @@ -20,6 +22,9 @@
}


TSemVer = TypeVar("TSemVer", bound="SemVer")


@attr.dataclass(frozen=True, slots=True)
class SemVer:
major: int
Expand All @@ -28,15 +33,13 @@ class SemVer:

schema: str = SCHEMA

def format(self) -> str: # noqa: A003
return format_version(
self.schema, SCHEMA_PARTS_FORMATTING, attr.asdict(self)
)

@classmethod
def from_parsed_dict(
cls, parsed: DictStrStr, *, schema: str = None
) -> "SemVer":
cls: Type[TSemVer],
parsed: DictStrStr,
*,
schema: Union[str, None] = None,
) -> TSemVer:
return cls(
major=int(parsed["major"]),
minor=int(parsed["minor"]),
Expand All @@ -45,16 +48,25 @@ def from_parsed_dict(
)

@classmethod
def initial(cls, *, schema: str = None) -> "SemVer":
def initial(
cls: Type[TSemVer], *, schema: Union[str, None] = None
) -> TSemVer:
return cls(major=1, minor=0, patch=0, schema=schema or SCHEMA)

@classmethod
def parse(cls, value: str, *, schema: str = None) -> "SemVer":
def parse(
cls: Type[TSemVer], value: str, *, schema: Union[str, None] = None
) -> TSemVer:
maybe_parsed = parse_version(SCHEMA, SCHEMA_PARTS_PARSING, value)
if maybe_parsed:
return cls.from_parsed_dict(maybe_parsed, schema=schema)
raise VersionParseError(schema or SCHEMA, value)

def format(self) -> str: # noqa: A003
return format_version(
self.schema, SCHEMA_PARTS_FORMATTING, attr.asdict(self)
)

def update(self, config: UpdateConfig) -> "SemVer":
if config.is_pre_release:
return self
Expand Down

0 comments on commit 5465292

Please sign in to comment.