Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit autoupdate (#209)
Browse files Browse the repository at this point in the history
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/asottile/pyupgrade: v2.34.0 → v2.37.1](asottile/pyupgrade@v2.34.0...v2.37.1)

* add ci clause

* fix package metadata

* forward ref on hint

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Talley Lambert <talley.lambert@gmail.com>
  • Loading branch information
pre-commit-ci[bot] and tlambert03 authored Jul 15, 2022
1 parent e78d3b8 commit 2fd59e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
ci:
autoupdate_schedule: monthly
autofix_commit_msg: "style: [pre-commit.ci] auto fixes [...]"
autoupdate_commit_msg: "ci: [pre-commit.ci] autoupdate"

exclude: _docs/example_plugin/some_module.py

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
Expand Down Expand Up @@ -32,7 +38,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v2.34.0
rev: v2.37.1
hooks:
- id: pyupgrade
args: [--py38-plus, --keep-runtime-typing]
Expand Down
22 changes: 13 additions & 9 deletions npe2/manifest/_package_metadata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from importlib.metadata import metadata
from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Union
from importlib import metadata
from typing import Dict, List, Literal, Optional, Union

from pydantic import BaseModel, Extra, Field, constr, root_validator
from pydantic.fields import SHAPE_LIST

if TYPE_CHECKING:
import email.message

# https://packaging.python.org/specifications/core-metadata/

MetadataVersion = Literal["1.0", "1.1", "1.2", "2.0", "2.1", "2.2"]
Expand Down Expand Up @@ -184,14 +181,21 @@ def _validate_root(cls, values):
@classmethod
def for_package(cls, name: str) -> "PackageMetadata":
"""Get PackageMetadata from a package name."""
return cls.from_dist_metadata(metadata(name))
return cls.from_dist_metadata(metadata.metadata(name))

# note, the metadata.PackageMetadata hint is only valid for py 3.10
# before that, it was email.message.Message
@classmethod
def from_dist_metadata(cls, meta: "email.message.Message") -> "PackageMetadata":
"""Accepts importlib.metadata.Distribution.metadata"""
def from_dist_metadata(cls, meta: "metadata.PackageMetadata") -> "PackageMetadata":
"""Generate PackageMetadata from importlib.metadata.PackageMetdata."""
manys = [f.name for f in cls.__fields__.values() if f.shape == SHAPE_LIST]
d: Dict[str, Union[str, List[str]]] = {}
for key, value in meta.items():
# looks like py3.10 changed the public protocol of metadata.PackageMetadata
# and they don't want you to rely on the Mapping interface... however, the
# __iter__ method doesn't iterate key value pairs, just keys, and I can't figure
# out how to get multi-valued fields from that (e.g. Classifier)
# might need to change this in the future
for key, value in meta.items(): # type: ignore
key = _norm(key)
if key in manys:
d.setdefault(key, []).append(value) # type: ignore
Expand Down

0 comments on commit 2fd59e5

Please sign in to comment.