Skip to content

Commit

Permalink
Merge pull request #606 from maresb/replace-black-with-ruff-format
Browse files Browse the repository at this point in the history
General infrastructure cleanup
  • Loading branch information
maresb authored Feb 11, 2024
2 parents 4bac435 + e6cbb81 commit 8284506
Show file tree
Hide file tree
Showing 26 changed files with 897 additions and 454 deletions.
45 changes: 30 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,54 @@
ci:
autofix_prs: false

exclude: ^conda_lock/_vendor/.*$

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: "^.*\\.patch$"
exclude: |
(?x)^(
.*\.patch$
| ^conda_lock/_vendor/
)
- id: check-ast

- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
language_version: python3
exclude: ^conda_lock/_vendor/conda/_vendor/appdirs\.py$

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
- types-filelock
- types-requests
- types-toml
- types-PyYAML
- gitpython
- jinja2
- pydantic
- pytest
- ruamel.yaml
- types-click
- types-click-default-group
- types-colorama
- types-filelock
- types-freezegun
- types-requests
- types-pygments
- types-pyyaml
- types-setuptools
- pydantic
exclude: ^(tests/test-local-pip/setup.py$|tests/test_conda_lock.py)
- types-toml
# pre-commit provides all the files explicitly so setting exclude in mypy.ini doesn't work.
exclude: |
(?x)^(
^conda_lock/_vendor/.*\.pyi$
| ^tests/test-local-pip/setup\.py$
| ^tests/test-pip-repositories/fake-private-package-1\.0\.0/setup\.py$
)
# First exclude is due to:
# conda_lock/_vendor/conda/__init__.py: error: Duplicate module named "conda_lock._vendor.conda" (also at "conda_lock/_vendor/conda.pyi")
# Second two excludes are due to:
# tests/test-pip-repositories/fake-private-package-1.0.0/setup.py: error: Duplicate module named "setup" (also at "tests/test-local-pip/setup.py")
2 changes: 1 addition & 1 deletion conda_lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

try:
__version__ = distribution("conda_lock").version
except Exception:
except Exception: # noqa: BLE001
__version__ = "unknown"
2 changes: 1 addition & 1 deletion conda_lock/_vendor/conda/_vendor/appdirs.py

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

8 changes: 4 additions & 4 deletions conda_lock/click_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from typing import Any, Mapping, Optional
from typing import Any, Dict, Optional

import click

Expand All @@ -10,12 +10,12 @@ class OrderedGroup(DefaultGroup):
def __init__(
self,
name: Optional[str] = None,
commands: Optional[Mapping[str, click.Command]] = None,
commands: Optional[Dict[str, click.Command]] = None,
**kwargs: Any,
):
super(OrderedGroup, self).__init__(name, commands, **kwargs)
super().__init__(name, commands, **kwargs)
#: the registered subcommands by their exported names.
self.commands = commands or OrderedDict()

def list_commands(self, ctx: click.Context) -> Mapping[str, click.Command]:
def list_commands(self, ctx: click.Context) -> Dict[str, click.Command]:
return self.commands
4 changes: 2 additions & 2 deletions conda_lock/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_in(


def read_file(filepath: Union[str, pathlib.Path]) -> str:
with open(filepath, mode="r") as fp:
with open(filepath) as fp:
return fp.read()


Expand All @@ -67,7 +67,7 @@ def temporary_file_with_contents(content: str) -> Iterator[pathlib.Path]:


def read_json(filepath: Union[str, pathlib.Path]) -> Dict:
with open(filepath, mode="r") as fp:
with open(filepath) as fp:
return json.load(fp)


Expand Down
2 changes: 1 addition & 1 deletion conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ def lock(
metadata_yamls = [pathlib.Path(path) for path in metadata_yamls]

# bail out if we do not encounter the default file if no files were passed
if ctx.get_parameter_source("files") == click.core.ParameterSource.DEFAULT:
if ctx.get_parameter_source("files") == click.core.ParameterSource.DEFAULT: # type: ignore
candidates = list(files)
candidates += [f.with_name(f.name.replace(".yml", ".yaml")) for f in candidates]
for f in candidates:
Expand Down
6 changes: 3 additions & 3 deletions conda_lock/conda_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def solve_specs_for_arch(
args.extend(["--channel", "msys2"])
args.extend(specs)
logger.info("%s using specs %s", platform, specs)
proc = subprocess.run(
proc = subprocess.run( # noqa: UP022 # Poetry monkeypatch breaks capture_output
[str(arg) for arg in args],
env=conda_env_override(platform),
stdout=subprocess.PIPE,
Expand Down Expand Up @@ -436,7 +436,7 @@ def update_specs_for_arch(
)
)
}
spec_for_name = {MatchSpec(v).name: v for v in specs} # type: ignore
spec_for_name = {MatchSpec(v).name: v for v in specs} # pyright: ignore
to_update = [
spec_for_name[name] for name in set(installed).intersection(update)
]
Expand Down Expand Up @@ -476,7 +476,7 @@ def update_specs_for_arch(
"update" if is_micromamba(conda) else "install",
*_get_conda_flags(channels=channels, platform=platform),
]
proc = subprocess.run(
proc = subprocess.run( # noqa: UP022 # Poetry monkeypatch breaks capture_output
[
str(arg)
for arg in [*args, "-p", prefix, "--json", "--dry-run", *to_update]
Expand Down
2 changes: 1 addition & 1 deletion conda_lock/lockfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def apply_categories(
by_category = defaultdict(list)

def extract_planned_items(
planned_items: Union[List[LockedDependency], LockedDependency]
planned_items: Union[List[LockedDependency], LockedDependency],
) -> List[LockedDependency]:
if not isinstance(planned_items, list):
return [planned_items]
Expand Down
12 changes: 7 additions & 5 deletions conda_lock/lockfile/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def create(
) -> "GitMeta | None":
try:
import git
import git.exc
except ImportError:
return None

Expand All @@ -129,12 +130,13 @@ def create(
git_user_email: "str | None" = None

try:
repo = git.Repo(search_parent_directories=True) # type: ignore
repo = git.Repo(search_parent_directories=True)
if MetadataOption.GitSha in metadata_choices:
most_recent_datetime: Optional[datetime.datetime] = None
for src_file in src_files:
relative_src_file_path = relative_path(
pathlib.Path(repo.working_tree_dir), src_file # type: ignore
pathlib.Path(repo.working_tree_dir), # type: ignore
src_file,
)
commit = list(
repo.iter_commits(paths=relative_src_file_path, max_count=1)
Expand All @@ -157,7 +159,7 @@ def create(
git_user_name = repo.config_reader().get_value("user", "name", None) # type: ignore
if MetadataOption.GitUserEmail in metadata_choices:
git_user_email = repo.config_reader().get_value("user", "email", None) # type: ignore
except git.exc.InvalidGitRepositoryError: # type: ignore
except git.exc.InvalidGitRepositoryError:
pass

if any([git_sha, git_user_name, git_user_email]):
Expand Down Expand Up @@ -282,13 +284,13 @@ def __or__(self, other: "LockMeta") -> "LockMeta":

@validator("channels", pre=True, always=True)
def ensure_channels(cls, v: List[Union[str, Channel]]) -> List[Channel]:
res = []
res: List[Channel] = []
for e in v:
if isinstance(e, str):
res.append(Channel.from_string(e))
else:
res.append(e)
return typing.cast(List[Channel], res)
return res


class Lockfile(StrictModel):
Expand Down
4 changes: 1 addition & 3 deletions conda_lock/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class MappingEntry(TypedDict):


class _LookupLoader:
_mapping_url: str = (
"https://raw.githubusercontent.com/regro/cf-graph-countyfair/master/mappings/pypi/grayskull_pypi_mapping.yaml"
)
_mapping_url: str = "https://raw.githubusercontent.com/regro/cf-graph-countyfair/master/mappings/pypi/grayskull_pypi_mapping.yaml"

@property
def mapping_url(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion conda_lock/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __repr_args__(self: BaseModel) -> "ReprArgs":


class Channel(ZeroValRepr, BaseModel):
model_config = ConfigDict(frozen=True) # type: ignore
model_config = ConfigDict(frozen=True)

url: str
used_env_vars: FrozenSet[str] = Field(default=frozenset())
Expand Down
6 changes: 3 additions & 3 deletions conda_lock/pypi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def get_marker_env(self) -> Dict[str, str]:


def _extract_glibc_version_from_virtual_packages(
platform_virtual_packages: Dict[str, dict]
platform_virtual_packages: Dict[str, dict],
) -> Optional[Version]:
"""Get the glibc version from the "package" repodata of a chosen platform.
Expand Down Expand Up @@ -189,7 +189,7 @@ def _glibc_version_from_manylinux_tag(tag: str) -> Version:


def _compute_compatible_manylinux_tags(
platform_virtual_packages: Optional[Dict[str, dict]]
platform_virtual_packages: Optional[Dict[str, dict]],
) -> List[str]:
"""Determine the manylinux tags that are compatible with the given platform.
Expand Down Expand Up @@ -479,7 +479,7 @@ def solve_pypi(
installed=installed,
locked=locked,
# ConsoleIO type is expected, but NullIO may be given:
io=io, # type: ignore
io=io, # pyright: ignore
)
to_update = list(
{spec.name for spec in pip_locked.values()}.intersection(use_latest)
Expand Down
11 changes: 5 additions & 6 deletions conda_lock/src_parser/conda_common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Optional

from conda_lock._vendor.conda.models.channel import Channel
from conda_lock._vendor.conda.models.match_spec import MatchSpec
from conda_lock.models.lock_spec import VersionedDependency

from .._vendor.conda.models.channel import Channel
from .._vendor.conda.models.match_spec import MatchSpec


def conda_spec_to_versioned_dep(spec: str, category: str) -> VersionedDependency:
"""Convert a string form conda spec into a versioned dependency for a given category.
Expand All @@ -13,8 +12,8 @@ def conda_spec_to_versioned_dep(spec: str, category: str) -> VersionedDependency
"""

try:
ms = MatchSpec(spec) # type: ignore # This is done in the metaclass for the matchspec
except Exception as e:
ms = MatchSpec(spec) # pyright: ignore # This is done in the metaclass for the matchspec
except Exception as e: # noqa: BLE001
raise RuntimeError(f"Failed to turn `{spec}` into a MatchSpec") from e

package_channel: Optional[Channel] = ms.get("channel")
Expand All @@ -24,7 +23,7 @@ def conda_spec_to_versioned_dep(spec: str, category: str) -> VersionedDependency
channel_str = None
return VersionedDependency(
name=ms.name,
version=ms.get("version", ""),
version=ms.get("version") or "",
manager="conda",
category=category,
extras=[],
Expand Down
7 changes: 4 additions & 3 deletions conda_lock/src_parser/meta_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ def __getattr__(self, k: str) -> "UndefinedNeverFail":
try:
return object.__getattr__(self, k) # type: ignore
except AttributeError:
return self._return_undefined(self._undefined_name + "." + k) # type: ignore
assert self._undefined_name is not None
return self._return_undefined(self._undefined_name + "." + k)

# Unlike the methods above, Python requires that these
# few methods must always return the correct type
__str__ = __repr__ = lambda self: self._return_value(str()) # type: ignore
__str__ = __repr__ = lambda self: self._return_value("") # type: ignore
__unicode__ = lambda self: self._return_value("") # noqa: E731
__int__ = lambda self: self._return_value(0) # type: ignore # noqa: E731
__float__ = lambda self: self._return_value(0.0) # type: ignore # noqa: E731
__nonzero__ = lambda self: self._return_value(False) # noqa: E731

def _return_undefined(self, result_name: str) -> "UndefinedNeverFail": # type: ignore
def _return_undefined(self, result_name: str) -> "UndefinedNeverFail":
# Record that this undefined variable was actually used.
UndefinedNeverFail.all_undefined_names.append(self._undefined_name)
return UndefinedNeverFail(
Expand Down
4 changes: 2 additions & 2 deletions conda_lock/src_parser/pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ def parse_python_requirement(
vcs="git",
rev=rev,
)
elif parsed_req.url: # type: ignore[attr-defined]
elif parsed_req.url:
assert conda_version in {"", "*", None}
url, frag = urldefrag(parsed_req.url) # type: ignore[attr-defined]
url, frag = urldefrag(parsed_req.url)
return URLDependency(
name=conda_dep_name,
manager=manager,
Expand Down
Loading

0 comments on commit 8284506

Please sign in to comment.