1
+ """
2
+ APIs exposing metadata from third-party Python packages.
3
+
4
+ This codebase is shared between importlib.metadata in the stdlib
5
+ and importlib_metadata in PyPI. See
6
+ https://github.com/python/importlib_metadata/wiki/Development-Methodology
7
+ for more detail.
8
+ """
9
+
1
10
from __future__ import annotations
2
11
3
- import os
4
- import re
5
12
import abc
6
- import sys
7
- import json
8
- import pipenv .vendor .zipp as zipp
13
+ import collections
9
14
import email
10
- import types
11
- import pathlib
12
- import operator
13
- import textwrap
14
15
import functools
15
16
import itertools
17
+ import operator
18
+ import os
19
+ import pathlib
16
20
import posixpath
17
- import collections
21
+ import re
22
+ import sys
23
+ import textwrap
24
+ import types
25
+ from contextlib import suppress
26
+ from importlib import import_module
27
+ from importlib .abc import MetaPathFinder
28
+ from itertools import starmap
29
+ from typing import Any , Iterable , List , Mapping , Match , Optional , Set , cast
18
30
19
31
from . import _meta
20
- from .compat import py39 , py311
21
32
from ._collections import FreezableDefaultDict , Pair
22
33
from ._compat import (
23
34
NullFinder ,
26
37
from ._functools import method_cache , pass_none
27
38
from ._itertools import always_iterable , bucket , unique_everseen
28
39
from ._meta import PackageMetadata , SimplePath
29
-
30
- from contextlib import suppress
31
- from importlib import import_module
32
- from importlib .abc import MetaPathFinder
33
- from itertools import starmap
34
- from typing import Any , Iterable , List , Mapping , Match , Optional , Set , cast
40
+ from .compat import py39 , py311
35
41
36
42
__all__ = [
37
43
'Distribution' ,
@@ -57,7 +63,7 @@ def __str__(self) -> str:
57
63
return f"No package metadata was found for { self .name } "
58
64
59
65
@property
60
- def name (self ) -> str : # type: ignore[override]
66
+ def name (self ) -> str : # type: ignore[override] # make readonly
61
67
(name ,) = self .args
62
68
return name
63
69
@@ -275,7 +281,7 @@ class EntryPoints(tuple):
275
281
276
282
__slots__ = ()
277
283
278
- def __getitem__ (self , name : str ) -> EntryPoint : # type: ignore[override]
284
+ def __getitem__ (self , name : str ) -> EntryPoint : # type: ignore[override] # Work with str instead of int
279
285
"""
280
286
Get the EntryPoint in self matching name.
281
287
"""
@@ -331,7 +337,7 @@ class PackagePath(pathlib.PurePosixPath):
331
337
size : int
332
338
dist : Distribution
333
339
334
- def read_text (self , encoding : str = 'utf-8' ) -> str : # type: ignore[override]
340
+ def read_text (self , encoding : str = 'utf-8' ) -> str :
335
341
return self .locate ().read_text (encoding = encoding )
336
342
337
343
def read_binary (self ) -> bytes :
@@ -666,6 +672,9 @@ def origin(self):
666
672
return self ._load_json ('direct_url.json' )
667
673
668
674
def _load_json (self , filename ):
675
+ # Deferred for performance (python/importlib_metadata#503)
676
+ import json
677
+
669
678
return pass_none (json .loads )(
670
679
self .read_text (filename ),
671
680
object_hook = lambda data : types .SimpleNamespace (** data ),
@@ -750,7 +759,7 @@ class FastPath:
750
759
True
751
760
"""
752
761
753
- @functools .lru_cache () # type: ignore
762
+ @functools .lru_cache () # type: ignore[misc]
754
763
def __new__ (cls , root ):
755
764
return super ().__new__ (cls )
756
765
@@ -768,7 +777,10 @@ def children(self):
768
777
return []
769
778
770
779
def zip_children (self ):
771
- zip_path = zipp .Path (self .root )
780
+ # deferred for performance (python/importlib_metadata#502)
781
+ from pipenv .vendor .zipp .compat .overlay import zipfile
782
+
783
+ zip_path = zipfile .Path (self .root )
772
784
names = zip_path .root .namelist ()
773
785
self .joinpath = zip_path .joinpath
774
786
@@ -1108,7 +1120,7 @@ def _get_toplevel_name(name: PackagePath) -> str:
1108
1120
# Defer import of inspect for performance (python/cpython#118761)
1109
1121
import inspect
1110
1122
1111
- return _topmost (name ) or ( inspect .getmodulename (name ) or str (name ) )
1123
+ return _topmost (name ) or inspect .getmodulename (name ) or str (name )
1112
1124
1113
1125
1114
1126
def _top_level_inferred (dist ):
0 commit comments