Skip to content

Commit 58ee354

Browse files
committed
Drop support for Python 2.
1 parent 9642f4d commit 58ee354

16 files changed

+37
-263
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
test:
77
strategy:
88
matrix:
9-
python: [2.7, 3.6, 3.7, 3.8, 3.9]
9+
python: [3.6, 3.7, 3.8, 3.9]
1010
platform: [ubuntu-latest, macos-latest, windows-latest]
1111
runs-on: ${{ matrix.platform }}
1212
steps:

coverage.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ omit =
66
.tox/*/lib/python*/site-packages/*
77
*/tests/*.py
88
*/testing/*.py
9-
importlib_resources/_py${OMIT}.py
109
importlib_resources/__init__.py
1110
importlib_resources/_compat.py
1211
importlib_resources/abc.py

importlib_resources/__init__.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""Read resources contained within a package."""
22

3-
import sys
4-
53
from ._common import (
64
as_file, files,
75
)
86

7+
from importlib_resources._py3 import (
8+
Package,
9+
Resource,
10+
contents,
11+
is_resource,
12+
open_binary,
13+
open_text,
14+
path,
15+
read_binary,
16+
read_text,
17+
)
18+
from importlib_resources.abc import ResourceReader
19+
920
# For compatibility. Ref #88.
1021
# Also requires hook-importlib_resources.py (Ref #101).
1122
__import__('importlib_resources.trees')
@@ -25,29 +36,3 @@
2536
'read_binary',
2637
'read_text',
2738
]
28-
29-
30-
if sys.version_info >= (3,):
31-
from importlib_resources._py3 import (
32-
Package,
33-
Resource,
34-
contents,
35-
is_resource,
36-
open_binary,
37-
open_text,
38-
path,
39-
read_binary,
40-
read_text,
41-
)
42-
from importlib_resources.abc import ResourceReader
43-
else:
44-
from importlib_resources._py2 import (
45-
contents,
46-
is_resource,
47-
open_binary,
48-
open_text,
49-
path,
50-
read_binary,
51-
read_text,
52-
)
53-
del __all__[:3]

importlib_resources/_common.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
from __future__ import absolute_import
2-
31
import os
42
import tempfile
53
import contextlib
64
import types
75
import importlib
6+
from pathlib import Path
7+
from functools import singledispatch
88

9-
from ._compat import (
10-
Path, FileNotFoundError,
11-
singledispatch, package_spec,
12-
)
9+
from ._compat import package_spec
1310

1411
if False: # TYPE_CHECKING
1512
from typing import Union, Any, Optional

importlib_resources/_compat.py

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
1-
from __future__ import absolute_import
1+
import abc
22
import sys
3+
from contextlib import suppress
34

45
# flake8: noqa
56

6-
if sys.version_info > (3,5):
7-
from pathlib import Path, PurePath
8-
else:
9-
from pathlib2 import Path, PurePath # type: ignore
10-
11-
12-
if sys.version_info > (3,):
13-
from contextlib import suppress
14-
else:
15-
from contextlib2 import suppress # type: ignore
16-
17-
18-
try:
19-
from functools import singledispatch
20-
except ImportError:
21-
from singledispatch import singledispatch # type: ignore
22-
23-
24-
try:
25-
from abc import ABC # type: ignore
26-
except ImportError:
27-
from abc import ABCMeta
28-
29-
class ABC(object): # type: ignore
30-
__metaclass__ = ABCMeta
31-
32-
33-
try:
34-
FileNotFoundError = FileNotFoundError # type: ignore
35-
except NameError:
36-
FileNotFoundError = OSError # type: ignore
37-
38-
39-
try:
40-
NotADirectoryError = NotADirectoryError # type: ignore
41-
except NameError:
42-
NotADirectoryError = OSError # type: ignore
43-
44-
457
try:
468
from zipfile import Path as ZipPath # type: ignore
479
except ImportError:
@@ -58,15 +20,7 @@ def runtime_checkable(cls): # type: ignore
5820
try:
5921
from typing import Protocol # type: ignore
6022
except ImportError:
61-
Protocol = ABC # type: ignore
62-
63-
64-
__metaclass__ = type
65-
66-
67-
class PackageSpec:
68-
def __init__(self, **kwargs):
69-
vars(self).update(kwargs)
23+
Protocol = abc.ABC # type: ignore
7024

7125

7226
class TraversableResourcesAdapter:
@@ -88,7 +42,6 @@ def __init__(self, spec):
8842

8943
@property
9044
def path(self):
91-
# Python < 3
9245
return self.spec.origin
9346

9447
def get_resource_reader(self, name):
@@ -129,11 +82,4 @@ def package_spec(package):
12982
matching the interfaces this library relies upon
13083
in later Python versions.
13184
"""
132-
spec = getattr(package, '__spec__', None) or \
133-
PackageSpec(
134-
origin=package.__file__,
135-
loader=getattr(package, '__loader__', None),
136-
name=package.__name__,
137-
submodule_search_locations=getattr(package, '__path__', None),
138-
)
139-
return TraversableResourcesAdapter(spec)
85+
return TraversableResourcesAdapter(package.__spec__)

importlib_resources/_py2.py

Lines changed: 0 additions & 107 deletions
This file was deleted.

importlib_resources/abc.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from __future__ import absolute_import
2-
31
import abc
42

5-
from ._compat import ABC, FileNotFoundError, runtime_checkable, Protocol
3+
from ._compat import runtime_checkable, Protocol
64

75
# Use mypy's comment syntax for Python 2 compatibility
86
try:
@@ -11,7 +9,7 @@
119
pass
1210

1311

14-
class ResourceReader(ABC):
12+
class ResourceReader(metaclass=abc.ABCMeta):
1513
"""Abstract base class for loaders to provide resource reading support."""
1614

1715
@abc.abstractmethod

importlib_resources/readers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os.path
22

33
from collections import OrderedDict
4+
from pathlib import Path
45

56
from . import abc
67

7-
from ._compat import Path, ZipPath
8-
from ._compat import FileNotFoundError, NotADirectoryError
8+
from ._compat import ZipPath
99

1010

1111
class FileReader(abc.TraversableResources):

importlib_resources/tests/_compat.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
try:
2-
from test.support import import_helper # type: ignore
3-
except ImportError:
4-
try:
5-
# Python 3.9 and earlier
6-
class import_helper: # type: ignore
7-
from test.support import modules_setup, modules_cleanup
8-
except ImportError:
9-
from . import py27compat
10-
11-
class import_helper: # type: ignore
12-
modules_setup = staticmethod(py27compat.modules_setup)
13-
modules_cleanup = staticmethod(py27compat.modules_cleanup)
1+
import os
142

153

164
try:
17-
from os import fspath # type: ignore
5+
from test.support import import_helper # type: ignore
186
except ImportError:
19-
# Python 3.5
20-
fspath = str # type: ignore
7+
# Python 3.9 and earlier
8+
class import_helper: # type: ignore
9+
from test.support import modules_setup, modules_cleanup
2110

2211

2312
try:
@@ -27,4 +16,4 @@ class import_helper: # type: ignore
2716
from test.support import unlink as _unlink
2817

2918
def unlink(target):
30-
return _unlink(fspath(target))
19+
return _unlink(os.fspath(target))

importlib_resources/tests/py27compat.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

importlib_resources/tests/test_open.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import importlib_resources as resources
55
from . import data01
66
from . import util
7-
from .._compat import FileNotFoundError
87

98

109
class CommonBinaryTests(util.CommonTests, unittest.TestCase):

importlib_resources/tests/test_reader.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from importlib import import_module
66
from importlib_resources.readers import MultiplexedPath, NamespaceReader
77

8-
from .._compat import FileNotFoundError, NotADirectoryError
9-
108

119
class MultiplexedPathTest(unittest.TestCase):
1210
@classmethod

0 commit comments

Comments
 (0)