Skip to content

Commit

Permalink
complete mypy transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed May 29, 2022
1 parent e09403f commit b45e19f
Show file tree
Hide file tree
Showing 35 changed files with 1,048 additions and 699 deletions.
8 changes: 5 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.0.1
rev: v3.1.0
hooks:
- id: reorder-python-imports
args: [ "--application-directories=.:src" , --py3-plus]
Expand All @@ -32,10 +32,12 @@ repos:
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.942'
rev: 'v0.950'
hooks:
- id: mypy
args: [--strict]
language_version: "3.10"
additional_dependencies:
- types-setuptools
- tokenize-rt==3.2.0
- pytest == 6.2.5
- pytest == 7.1
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
"""
import os
import sys
from typing import NoReturn
from typing import Optional

import setuptools
from setuptools.command.bdist_egg import bdist_egg as original_bdist_egg


class bdist_egg(original_bdist_egg):
def run(self):
def run(self) -> NoReturn:
raise SystemExit(
"%s is forbidden, "
"please update to setuptools>=45 which uses pip" % type(self).__name__
)


def scm_version():
def scm_version() -> str:

if sys.version_info < (3, 6):
raise RuntimeError(
Expand All @@ -40,8 +42,11 @@ def scm_version():
from setuptools_scm import git
from setuptools_scm import hg
from setuptools_scm.version import guess_next_dev_version, get_local_node_and_date
from setuptools_scm.config import Configuration

def parse(root, config):
from setuptools_scm.version import ScmVersion

def parse(root: str, config: Configuration) -> Optional[ScmVersion]:
try:
return parse_pkginfo(root, config)
except OSError:
Expand Down
53 changes: 28 additions & 25 deletions src/setuptools_scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"""
import os
import warnings
from typing import Any
from typing import Callable
from typing import Optional
from typing import TYPE_CHECKING
from typing import Union

from . import _types
from . import _types as _t
from ._entrypoints import _call_entrypoint_fn
from ._entrypoints import _version_from_entrypoints
from ._overrides import _read_pretended_version_for
Expand Down Expand Up @@ -36,14 +39,14 @@
# coding: utf-8
# file generated by setuptools_scm
# don't change, don't track in version control
version = {version!r}
version_tuple = {version_tuple!r}
__version__ = version = {version!r}
__version_tuple__ = version_tuple = {version_tuple!r}
""",
".txt": "{version}",
}


def version_from_scm(root):
def version_from_scm(root: _t.PathT) -> Optional[ScmVersion]:
warnings.warn(
"version_from_scm is deprecated please use get_version",
category=DeprecationWarning,
Expand All @@ -54,11 +57,11 @@ def version_from_scm(root):


def dump_version(
root: _types.PathT,
root: _t.PathT,
version: str,
write_to: _types.PathT,
write_to: _t.PathT,
template: "str | None" = None,
):
) -> None:
assert isinstance(version, str)
target = os.path.normpath(os.path.join(root, write_to))
ext = os.path.splitext(target)[1]
Expand Down Expand Up @@ -102,7 +105,7 @@ def _do_parse(config: Configuration) -> "ScmVersion|None":
return version


def _version_missing(config) -> "NoReturn":
def _version_missing(config: Configuration) -> "NoReturn":
raise LookupError(
f"setuptools-scm was unable to detect version for {config.absolute_root}.\n\n"
"Make sure you're either building from a fully intact git repository "
Expand All @@ -116,23 +119,23 @@ def _version_missing(config) -> "NoReturn":


def get_version(
root=".",
version_scheme=DEFAULT_VERSION_SCHEME,
local_scheme=DEFAULT_LOCAL_SCHEME,
write_to=None,
write_to_template=None,
relative_to=None,
tag_regex=DEFAULT_TAG_REGEX,
parentdir_prefix_version=None,
fallback_version=None,
fallback_root=".",
parse=None,
git_describe_command=None,
dist_name=None,
version_cls=None,
normalize=True,
search_parent_directories=False,
):
root: str = ".",
version_scheme: Union[Callable[[ScmVersion], str], str] = DEFAULT_VERSION_SCHEME,
local_scheme: Union[Callable[[ScmVersion], str], str] = DEFAULT_LOCAL_SCHEME,
write_to: Optional[_t.PathT] = None,
write_to_template: Optional[str] = None,
relative_to: Optional[str] = None,
tag_regex: str = DEFAULT_TAG_REGEX,
parentdir_prefix_version: Optional[str] = None,
fallback_version: Optional[str] = None,
fallback_root: _t.PathT = ".",
parse: Optional[Any] = None,
git_describe_command: Optional[Any] = None,
dist_name: Optional[str] = None,
version_cls: Optional[Any] = None,
normalize: bool = True,
search_parent_directories: bool = False,
) -> str:
"""
If supplied, relative_to should be a file from which root may
be resolved. Typically called by a script or module that is not
Expand Down
81 changes: 1 addition & 80 deletions src/setuptools_scm/__main__.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,4 @@
import argparse
import os
import sys

from setuptools_scm import _get_version
from setuptools_scm.config import Configuration
from setuptools_scm.discover import walk_potential_roots
from setuptools_scm.integration import find_files


def main() -> None:
opts = _get_cli_opts()
root = opts.root or "."

try:
pyproject = opts.config or _find_pyproject(root)
root = opts.root or os.path.relpath(os.path.dirname(pyproject))
config = Configuration.from_file(pyproject, root=root)
except (LookupError, FileNotFoundError) as ex:
# no pyproject.toml OR no [tool.setuptools_scm]
print(
f"Warning: could not use {os.path.relpath(pyproject)},"
" using default configuration.\n"
f" Reason: {ex}.",
file=sys.stderr,
)
config = Configuration(root=root)

version = _get_version(config)
assert version is not None
if opts.strip_dev:
version = version.partition(".dev")[0]
print(version)

if opts.command == "ls":
for fname in find_files(config.root):
print(fname)


def _get_cli_opts() -> argparse.Namespace:
prog = "python -m setuptools_scm"
desc = "Print project version according to SCM metadata"
parser = argparse.ArgumentParser(prog, description=desc)
# By default, help for `--help` starts with lower case, so we keep the pattern:
parser.add_argument(
"-r",
"--root",
default=None,
help='directory managed by the SCM, default: inferred from config file, or "."',
)
parser.add_argument(
"-c",
"--config",
default=None,
metavar="PATH",
help="path to 'pyproject.toml' with setuptools_scm config, "
"default: looked up in the current or parent directories",
)
parser.add_argument(
"--strip-dev",
action="store_true",
help="remove the dev/local parts of the version before printing the version",
)
sub = parser.add_subparsers(title="extra commands", dest="command", metavar="")
# We avoid `metavar` to prevent printing repetitive information
desc = "List files managed by the SCM"
sub.add_parser("ls", help=desc[0].lower() + desc[1:], description=desc)
return parser.parse_args()


def _find_pyproject(parent: str) -> str:
for directory in walk_potential_roots(os.path.abspath(parent)):
pyproject = os.path.join(directory, "pyproject.toml")
if os.path.isfile(pyproject):
return pyproject

return os.path.abspath(
"pyproject.toml"
) # use default name to trigger the default errors

from ._cli import main

if __name__ == "__main__":
main()
80 changes: 80 additions & 0 deletions src/setuptools_scm/_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import argparse
import os
import sys

from setuptools_scm import _get_version
from setuptools_scm.config import Configuration
from setuptools_scm.discover import walk_potential_roots
from setuptools_scm.integration import find_files


def main() -> None:
opts = _get_cli_opts()
root = opts.root or "."

pyproject = opts.config or _find_pyproject(root)

try:
root = opts.root or os.path.relpath(os.path.dirname(pyproject))
config = Configuration.from_file(pyproject, root=root)
except (LookupError, FileNotFoundError) as ex:
# no pyproject.toml OR no [tool.setuptools_scm]
print(
f"Warning: could not use {os.path.relpath(pyproject)},"
" using default configuration.\n"
f" Reason: {ex}.",
file=sys.stderr,
)
config = Configuration(root=root)

version = _get_version(config)
assert version is not None
if opts.strip_dev:
version = version.partition(".dev")[0]
print(version)

if opts.command == "ls":
for fname in find_files(config.root):
print(fname)


def _get_cli_opts() -> argparse.Namespace:
prog = "python -m setuptools_scm"
desc = "Print project version according to SCM metadata"
parser = argparse.ArgumentParser(prog, description=desc)
# By default, help for `--help` starts with lower case, so we keep the pattern:
parser.add_argument(
"-r",
"--root",
default=None,
help='directory managed by the SCM, default: inferred from config file, or "."',
)
parser.add_argument(
"-c",
"--config",
default=None,
metavar="PATH",
help="path to 'pyproject.toml' with setuptools_scm config, "
"default: looked up in the current or parent directories",
)
parser.add_argument(
"--strip-dev",
action="store_true",
help="remove the dev/local parts of the version before printing the version",
)
sub = parser.add_subparsers(title="extra commands", dest="command", metavar="")
# We avoid `metavar` to prevent printing repetitive information
desc = "List files managed by the SCM"
sub.add_parser("ls", help=desc[0].lower() + desc[1:], description=desc)
return parser.parse_args()


def _find_pyproject(parent: str) -> str:
for directory in walk_potential_roots(os.path.abspath(parent)):
pyproject = os.path.join(directory, "pyproject.toml")
if os.path.isfile(pyproject):
return pyproject

return os.path.abspath(
"pyproject.toml"
) # use default name to trigger the default errors
Loading

0 comments on commit b45e19f

Please sign in to comment.