Skip to content

Commit

Permalink
Merge branch 'main' into fix-relative-to
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco authored Jun 23, 2024
2 parents 1584b96 + e8998d9 commit 52ce982
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 119 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ on:
merge_group:
push:
branches-ignore:
# disabled for jaraco/skeleton#103
# - gh-readonly-queue/** # Temporary merge queue-related GH-made branches
# temporary GH branches relating to merge queues (jaraco/skeleton#93)
- gh-readonly-queue/**
tags:
# required if branches-ignore is supplied (jaraco/skeleton#103)
- '**'
pull_request:

permissions:
Expand All @@ -28,10 +31,10 @@ env:
jobs:
test:
strategy:
# https://blog.jaraco.com/efficient-use-of-ci-resources/
matrix:
python:
- "3.8"
- "3.11"
- "3.12"
platform:
- ubuntu-latest
Expand All @@ -42,6 +45,8 @@ jobs:
platform: ubuntu-latest
- python: "3.10"
platform: ubuntu-latest
- python: "3.11"
platform: ubuntu-latest
- python: pypy3.10
platform: ubuntu-latest
runs-on: ${{ matrix.platform }}
Expand Down
6 changes: 5 additions & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ python:
install:
- path: .
extra_requirements:
- docs
- doc

# required boilerplate readthedocs/readthedocs.org#10401
build:
os: ubuntu-lts-latest
tools:
python: latest
# post-checkout job to ensure the clone isn't shallow jaraco/skeleton#114
jobs:
post_checkout:
- git fetch --unshallow || true
32 changes: 32 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
v7.2.0
======

Features
--------

- Deferred select imports in for speedup (python/cpython#109829).
- Updated fixtures for python/cpython#120801.


v7.1.0
======

Features
--------

- Improve import time (python/cpython#114664).


Bugfixes
--------

- Make MetadataPathFinder.find_distributions a classmethod for consistency with CPython. Closes #484. (#484)
- Allow ``MetadataPathFinder.invalidate_caches`` to be called as a classmethod.


v7.0.2
======

No significant changes.


v7.0.1
======

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
url='https://peps.python.org/pep-{pep_number:0>4}/',
),
dict(
pattern=r'(Python #|py-)(?P<python>\d+)',
pattern=r'(python/cpython#|Python #|py-)(?P<python>\d+)',
url='https://github.com/python/cpython/issues/{python}',
),
],
Expand Down
32 changes: 20 additions & 12 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import re
import abc
import csv
import sys
import json
import zipp
Expand All @@ -19,8 +18,8 @@
import posixpath
import collections

from .compat.py311 import relative_fix
from . import _adapters, _meta, _py39compat
from . import _meta
from .compat import py39, py311
from ._collections import FreezableDefaultDict, Pair
from ._compat import (
NullFinder,
Expand All @@ -34,7 +33,7 @@
from importlib import import_module
from importlib.abc import MetaPathFinder
from itertools import starmap
from typing import Iterable, List, Mapping, Optional, Set, cast
from typing import Any, Iterable, List, Mapping, Match, Optional, Set, cast

__all__ = [
'Distribution',
Expand Down Expand Up @@ -176,12 +175,12 @@ class EntryPoint:
def __init__(self, name: str, value: str, group: str) -> None:
vars(self).update(name=name, value=value, group=group)

def load(self):
def load(self) -> Any:
"""Load the entry point from its definition. If only a module
is indicated by the value, return that module. Otherwise,
return the named object.
"""
match = self.pattern.match(self.value)
match = cast(Match, self.pattern.match(self.value))
module = import_module(match.group('module'))
attrs = filter(None, (match.group('attr') or '').split('.'))
return functools.reduce(getattr, attrs, module)
Expand Down Expand Up @@ -276,12 +275,12 @@ def __repr__(self):
"""
return '%s(%r)' % (self.__class__.__name__, tuple(self))

def select(self, **params):
def select(self, **params) -> EntryPoints:
"""
Select entry points from self that match the
given parameters (typically group and/or name).
"""
return EntryPoints(ep for ep in self if _py39compat.ep_matches(ep, **params))
return EntryPoints(ep for ep in self if py39.ep_matches(ep, **params))

@property
def names(self) -> Set[str]:
Expand Down Expand Up @@ -462,6 +461,9 @@ def metadata(self) -> _meta.PackageMetadata:
Custom providers may provide the METADATA file or override this
property.
"""
# deferred for performance (python/cpython#109829)
from . import _adapters

opt_text = (
self.read_text('METADATA')
or self.read_text('PKG-INFO')
Expand Down Expand Up @@ -523,6 +525,10 @@ def make_file(name, hash=None, size_str=None):

@pass_none
def make_files(lines):
# Delay csv import, since Distribution.files is not as widely used
# as other parts of importlib.metadata
import csv

return starmap(make_file, csv.reader(lines))

@pass_none
Expand Down Expand Up @@ -564,7 +570,7 @@ def _read_files_egginfo_installed(self):
return

paths = (
relative_fix((subdir / name).resolve())
py311.relative_fix((subdir / name).resolve())
.relative_to(self.locate_file('').resolve(), walk_up=True)
.as_posix()
for name in text.splitlines()
Expand Down Expand Up @@ -873,8 +879,9 @@ class MetadataPathFinder(NullFinder, DistributionFinder):
of Python that do not have a PathFinder find_distributions().
"""

@classmethod
def find_distributions(
self, context=DistributionFinder.Context()
cls, context=DistributionFinder.Context()
) -> Iterable[PathDistribution]:
"""
Find distributions.
Expand All @@ -884,7 +891,7 @@ def find_distributions(
(or all names if ``None`` indicated) along the paths in the list
of directories ``context.path``.
"""
found = self._search_paths(context.name, context.path)
found = cls._search_paths(context.name, context.path)
return map(PathDistribution, found)

@classmethod
Expand All @@ -895,6 +902,7 @@ def _search_paths(cls, name, paths):
path.search(prepared) for path in map(FastPath, paths)
)

@classmethod
def invalidate_caches(cls) -> None:
FastPath.__new__.cache_clear()

Expand Down Expand Up @@ -992,7 +1000,7 @@ def version(distribution_name: str) -> str:

_unique = functools.partial(
unique_everseen,
key=_py39compat.normalized_name,
key=py39.normalized_name,
)
"""
Wrapper for ``distributions`` to return unique distributions by name.
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if TYPE_CHECKING: # pragma: no cover
# Prevent circular imports on runtime.
from . import Distribution, EntryPoint
from .. import Distribution, EntryPoint
else:
Distribution = EntryPoint = Any

Expand All @@ -18,7 +18,7 @@ def normalized_name(dist: Distribution) -> Optional[str]:
try:
return dist._normalized_name
except AttributeError:
from . import Prepared # -> delay to prevent circular imports.
from .. import Prepared # -> delay to prevent circular imports.

return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])

Expand All @@ -30,7 +30,7 @@ def ep_matches(ep: EntryPoint, **params) -> bool:
try:
return ep.matches(**params)
except AttributeError:
from . import EntryPoint # -> delay to prevent circular imports.
from .. import EntryPoint # -> delay to prevent circular imports.

# Reconstruct the EntryPoint object to make sure it is compatible.
return EntryPoint(ep.name, ep.value, ep.group).matches(**params)
59 changes: 58 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
[build-system]
requires = ["setuptools>=56", "setuptools_scm[toml]>=3.4.1"]
requires = ["setuptools>=61.2", "setuptools_scm[toml]>=3.4.1"]
build-backend = "setuptools.build_meta"

[project]
name = "importlib_metadata"
authors = [
{ name = "Jason R. Coombs", email = "[email protected]" },
]
description = "Read metadata from Python packages"
readme = "README.rst"
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
]
requires-python = ">=3.8"
dependencies = [
"zipp>=0.5",
'typing-extensions>=3.6.4; python_version < "3.8"',
]
dynamic = ["version"]

[project.urls]
Source = "https://github.com/python/importlib_metadata"

[project.optional-dependencies]
test = [
# upstream
"pytest >= 6, != 8.1.*",
"pytest-checkdocs >= 2.4",
"pytest-cov",
"pytest-mypy",
"pytest-enabler >= 2.2",
"pytest-ruff >= 0.2.1",

# local
'importlib_resources>=1.3; python_version < "3.9"',
"packaging",
"pyfakefs",
"flufl.flake8",
"pytest-perf >= 0.9.2",
"jaraco.test >= 5.4",
]
doc = [
# upstream
"sphinx >= 3.5",
"jaraco.packaging >= 9.3",
"rst.linker >= 1.9",
"furo",
"sphinx-lint",

# tidelift
"jaraco.tidelift >= 1.4",

# local
]
perf = ["ipython"]

[tool.setuptools_scm]
5 changes: 4 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[pytest]
norecursedirs=dist build .tox .eggs
addopts=--doctest-modules
addopts=
--doctest-modules
--import-mode importlib
consider_namespace_packages=true
filterwarnings=
## upstream

Expand Down
57 changes: 0 additions & 57 deletions setup.cfg

This file was deleted.

Empty file added tests/compat/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions tests/compat/py312.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import contextlib

from .py39 import import_helper


@contextlib.contextmanager
def isolated_modules():
"""
Save modules on entry and cleanup on exit.
"""
(saved,) = import_helper.modules_setup()
try:
yield
finally:
import_helper.modules_cleanup(saved)


vars(import_helper).setdefault('isolated_modules', isolated_modules)
Loading

0 comments on commit 52ce982

Please sign in to comment.