Skip to content

Commit 06a831d

Browse files
committed
Upgrade Python code to 3.10
1 parent 27cf856 commit 06a831d

24 files changed

Lines changed: 102 additions & 120 deletions

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#

src/grimp/adaptors/caching.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import json
44
import logging
5-
from typing import Dict, List, Optional, Set, Tuple, Type
5+
from typing import Optional
66

77
from grimp.application.ports.filesystem import AbstractFileSystem
88
from grimp.application.ports.modulefinder import FoundPackage, ModuleFile
@@ -13,7 +13,7 @@
1313
from grimp import _rustgrimp as rust # type: ignore[attr-defined]
1414

1515
logger = logging.getLogger(__name__)
16-
PrimitiveFormat = Dict[str, List[Tuple[str, Optional[int], str]]]
16+
PrimitiveFormat = dict[str, list[tuple[str, Optional[int], str]]]
1717

1818

1919
class CacheFileNamer:
@@ -24,7 +24,7 @@ def make_meta_file_name(cls, found_package: FoundPackage) -> str:
2424
@classmethod
2525
def make_data_file_name(
2626
cls,
27-
found_packages: Set[FoundPackage],
27+
found_packages: set[FoundPackage],
2828
include_external_packages: bool,
2929
exclude_type_checking_imports: bool,
3030
) -> str:
@@ -42,7 +42,7 @@ def make_data_file_name(
4242
@classmethod
4343
def make_data_file_unique_string(
4444
cls,
45-
found_packages: Set[FoundPackage],
45+
found_packages: set[FoundPackage],
4646
include_external_packages: bool,
4747
exclude_type_checking_imports: bool,
4848
) -> str:
@@ -65,24 +65,24 @@ def make_data_file_unique_string(
6565
class Cache(AbstractCache):
6666
DEFAULT_CACHE_DIR = ".grimp_cache"
6767

68-
def __init__(self, *args, namer: Type[CacheFileNamer], **kwargs) -> None:
68+
def __init__(self, *args, namer: type[CacheFileNamer], **kwargs) -> None:
6969
"""
7070
Don't instantiate Cache directly; use Cache.setup().
7171
"""
7272
super().__init__(*args, **kwargs)
73-
self._mtime_map: Dict[str, float] = {}
74-
self._data_map: Dict[Module, Set[DirectImport]] = {}
73+
self._mtime_map: dict[str, float] = {}
74+
self._data_map: dict[Module, set[DirectImport]] = {}
7575
self._namer = namer
7676

7777
@classmethod
7878
def setup(
7979
cls,
8080
file_system: AbstractFileSystem,
81-
found_packages: Set[FoundPackage],
81+
found_packages: set[FoundPackage],
8282
include_external_packages: bool,
8383
exclude_type_checking_imports: bool = False,
84-
cache_dir: Optional[str] = None,
85-
namer: Type[CacheFileNamer] = CacheFileNamer,
84+
cache_dir: str | None = None,
85+
namer: type[CacheFileNamer] = CacheFileNamer,
8686
) -> "Cache":
8787
cache = cls(
8888
file_system=file_system,
@@ -98,10 +98,10 @@ def setup(
9898
return cache
9999

100100
@classmethod
101-
def cache_dir_or_default(cls, cache_dir: Optional[str]) -> str:
101+
def cache_dir_or_default(cls, cache_dir: str | None) -> str:
102102
return cache_dir or cls.DEFAULT_CACHE_DIR
103103

104-
def read_imports(self, module_file: ModuleFile) -> Set[DirectImport]:
104+
def read_imports(self, module_file: ModuleFile) -> set[DirectImport]:
105105
try:
106106
cached_mtime = self._mtime_map[module_file.module.name]
107107
except KeyError:
@@ -118,7 +118,7 @@ def read_imports(self, module_file: ModuleFile) -> Set[DirectImport]:
118118

119119
def write(
120120
self,
121-
imports_by_module: Dict[Module, Set[DirectImport]],
121+
imports_by_module: dict[Module, set[DirectImport]],
122122
) -> None:
123123
self._write_marker_files_if_not_already_there()
124124
# Write data file.
@@ -165,13 +165,13 @@ def write(
165165
def _build_mtime_map(self) -> None:
166166
self._mtime_map = self._read_mtime_map_files()
167167

168-
def _read_mtime_map_files(self) -> Dict[str, float]:
169-
all_mtimes: Dict[str, float] = {}
168+
def _read_mtime_map_files(self) -> dict[str, float]:
169+
all_mtimes: dict[str, float] = {}
170170
for found_package in self.found_packages:
171171
all_mtimes.update(self._read_mtime_map_file(found_package))
172172
return all_mtimes
173173

174-
def _read_mtime_map_file(self, found_package: FoundPackage) -> Dict[str, float]:
174+
def _read_mtime_map_file(self, found_package: FoundPackage) -> dict[str, float]:
175175
meta_cache_filename = self.file_system.join(
176176
self.cache_dir, self._namer.make_meta_file_name(found_package)
177177
)
@@ -191,7 +191,7 @@ def _read_mtime_map_file(self, found_package: FoundPackage) -> Dict[str, float]:
191191
def _build_data_map(self) -> None:
192192
self._data_map = self._read_data_map_file()
193193

194-
def _read_data_map_file(self) -> Dict[Module, Set[DirectImport]]:
194+
def _read_data_map_file(self) -> dict[Module, set[DirectImport]]:
195195
data_cache_filename = self.file_system.join(
196196
self.cache_dir,
197197
self._namer.make_data_file_name(

src/grimp/adaptors/filesystem.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import tokenize
3-
from typing import Iterator, List, Tuple
3+
from collections.abc import Iterator
44

55
from grimp.application.ports.filesystem import AbstractFileSystem, BasicFileSystem
66
from grimp import _rustgrimp as rust # type: ignore[attr-defined]
@@ -18,13 +18,13 @@ def sep(self) -> str:
1818
def dirname(self, filename: str) -> str:
1919
return os.path.dirname(filename)
2020

21-
def walk(self, directory_name: str) -> Iterator[Tuple[str, List[str], List[str]]]:
21+
def walk(self, directory_name: str) -> Iterator[tuple[str, list[str], list[str]]]:
2222
yield from os.walk(directory_name, followlinks=True)
2323

2424
def join(self, *components: str) -> str:
2525
return os.path.join(*components)
2626

27-
def split(self, file_name: str) -> Tuple[str, str]:
27+
def split(self, file_name: str) -> tuple[str, str]:
2828
return os.path.split(file_name)
2929

3030
def read(self, file_name: str) -> str:

src/grimp/adaptors/modulefinder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Iterable, List
2+
from collections.abc import Iterable
33

44
from grimp.application.ports import modulefinder
55
from grimp.application.ports.filesystem import AbstractFileSystem
@@ -14,7 +14,7 @@ def find_package(
1414
) -> modulefinder.FoundPackage:
1515
self.file_system = file_system
1616

17-
module_files: List[modulefinder.ModuleFile] = []
17+
module_files: list[modulefinder.ModuleFile] = []
1818

1919
for module_filename in self._get_python_files_inside_package(package_directory):
2020
module_name = self._module_name_from_filename(

src/grimp/adaptors/packagefinder.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ def determine_package_directory(
1919
# Attempt to locate the package file.
2020
spec = importlib.util.find_spec(package_name)
2121
if not spec:
22-
logger.debug("sys.path: {}".format(sys.path))
23-
raise ValueError(
24-
"Could not find package '{}' in your Python path.".format(package_name)
25-
)
22+
logger.debug(f"sys.path: {sys.path}")
23+
raise ValueError(f"Could not find package '{package_name}' in your Python path.")
2624

2725
if spec.has_location and spec.origin:
2826
if not self._is_a_package(spec, file_system) or self._has_a_non_namespace_parent(spec):

src/grimp/application/graph.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
2-
from typing import List, Optional, Sequence, Set, Tuple, TypedDict
2+
from typing import TypedDict
3+
from collections.abc import Sequence
34
from grimp.domain.analysis import PackageDependency, Route
45
from grimp.domain.valueobjects import Layer
56
from grimp import _rustgrimp as rust # type: ignore[attr-defined]
@@ -18,7 +19,7 @@ class Import(TypedDict):
1819

1920
# Corresponds to importer, imported.
2021
# Prefer this form to Import, as it's both more lightweight, and hashable.
21-
ImportTuple = Tuple[str, str]
22+
ImportTuple = tuple[str, str]
2223

2324

2425
class DetailedImport(Import):
@@ -33,22 +34,22 @@ class ImportGraph:
3334

3435
def __init__(self) -> None:
3536
super().__init__()
36-
self._cached_modules: Set[str] | None = None
37+
self._cached_modules: set[str] | None = None
3738
self._rustgraph = rust.Graph()
3839

3940
# Mechanics
4041
# ---------
4142

4243
@property
43-
def modules(self) -> Set[str]:
44+
def modules(self) -> set[str]:
4445
"""
4546
The names of all the modules in the graph.
4647
"""
4748
if self._cached_modules is None:
4849
self._cached_modules = self._rustgraph.get_modules()
4950
return self._cached_modules
5051

51-
def find_matching_modules(self, expression: str) -> Set[str]:
52+
def find_matching_modules(self, expression: str) -> set[str]:
5253
"""
5354
Find all modules matching the passed expression.
5455
@@ -135,8 +136,8 @@ def add_import(
135136
*,
136137
importer: str,
137138
imported: str,
138-
line_number: Optional[int] = None,
139-
line_contents: Optional[str] = None,
139+
line_number: int | None = None,
140+
line_contents: str | None = None,
140141
) -> None:
141142
"""
142143
Add a direct import between two modules to the graph. If the modules are not already
@@ -166,7 +167,7 @@ def count_imports(self) -> int:
166167
# Descendants
167168
# -----------
168169

169-
def find_children(self, module: str) -> Set[str]:
170+
def find_children(self, module: str) -> set[str]:
170171
"""
171172
Find all modules one level below the module. For example, the children of
172173
foo.bar might be foo.bar.one and foo.bar.two, but not foo.bar.two.green.
@@ -180,7 +181,7 @@ def find_children(self, module: str) -> Set[str]:
180181
raise ValueError("Cannot find children of a squashed module.")
181182
return self._rustgraph.find_children(module)
182183

183-
def find_descendants(self, module: str) -> Set[str]:
184+
def find_descendants(self, module: str) -> set[str]:
184185
"""
185186
Find all modules below the module. For example, the descendants of
186187
foo.bar might be foo.bar.one and foo.bar.two and foo.bar.two.green.
@@ -213,16 +214,16 @@ def direct_import_exists(
213214
importer=importer, imported=imported, as_packages=as_packages
214215
)
215216

216-
def find_modules_directly_imported_by(self, module: str) -> Set[str]:
217+
def find_modules_directly_imported_by(self, module: str) -> set[str]:
217218
return self._rustgraph.find_modules_directly_imported_by(module)
218219

219-
def find_modules_that_directly_import(self, module: str) -> Set[str]:
220+
def find_modules_that_directly_import(self, module: str) -> set[str]:
220221
if self._rustgraph.contains_module(module):
221222
# TODO panics if module isn't in modules.
222223
return self._rustgraph.find_modules_that_directly_import(module)
223224
return set()
224225

225-
def get_import_details(self, *, importer: str, imported: str) -> List[DetailedImport]:
226+
def get_import_details(self, *, importer: str, imported: str) -> list[DetailedImport]:
226227
"""
227228
Return available metadata relating to the direct imports between two modules, in the form:
228229
[
@@ -246,7 +247,7 @@ def get_import_details(self, *, importer: str, imported: str) -> List[DetailedIm
246247
imported=imported,
247248
)
248249

249-
def find_matching_direct_imports(self, import_expression: str) -> List[Import]:
250+
def find_matching_direct_imports(self, import_expression: str) -> list[Import]:
250251
"""
251252
Find all direct imports matching the passed expressions.
252253
@@ -290,7 +291,7 @@ def find_matching_direct_imports(self, import_expression: str) -> List[Import]:
290291
# Indirect imports
291292
# ----------------
292293

293-
def find_downstream_modules(self, module: str, as_package: bool = False) -> Set[str]:
294+
def find_downstream_modules(self, module: str, as_package: bool = False) -> set[str]:
294295
"""
295296
Return a set of the names of all the modules that import (even indirectly) the
296297
supplied module name.
@@ -312,7 +313,7 @@ def find_downstream_modules(self, module: str, as_package: bool = False) -> Set[
312313
"""
313314
return self._rustgraph.find_downstream_modules(module, as_package)
314315

315-
def find_upstream_modules(self, module: str, as_package: bool = False) -> Set[str]:
316+
def find_upstream_modules(self, module: str, as_package: bool = False) -> set[str]:
316317
"""
317318
Return a set of the names of all the modules that are imported (even indirectly) by the
318319
supplied module.
@@ -352,7 +353,7 @@ def find_shortest_chain(
352353

353354
def find_shortest_chains(
354355
self, importer: str, imported: str, as_packages: bool = True
355-
) -> Set[Tuple[str, ...]]:
356+
) -> set[tuple[str, ...]]:
356357
"""
357358
Find the shortest import chains that exist between the importer and imported, and
358359
between any modules contained within them if as_packages is True. Only one chain per
@@ -474,7 +475,7 @@ def __repr__(self) -> str:
474475
stringified_modules = "empty"
475476
return f"<{self.__class__.__name__}: {stringified_modules}>"
476477

477-
def __deepcopy__(self, memodict: dict) -> "ImportGraph":
478+
def __deepcopy__(self, memodict: dict) -> ImportGraph:
478479
new_graph = ImportGraph()
479480
new_graph._rustgraph = self._rustgraph.clone()
480481
return new_graph

src/grimp/application/ports/caching.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict, Optional, Set
2-
31
from grimp.application.ports.modulefinder import FoundPackage, ModuleFile
42
from grimp.domain.valueobjects import DirectImport, Module
53

@@ -16,7 +14,7 @@ def __init__(
1614
file_system: AbstractFileSystem,
1715
include_external_packages: bool,
1816
exclude_type_checking_imports: bool,
19-
found_packages: Set[FoundPackage],
17+
found_packages: set[FoundPackage],
2018
cache_dir: str,
2119
) -> None:
2220
"""
@@ -32,11 +30,11 @@ def __init__(
3230
def setup(
3331
cls,
3432
file_system: AbstractFileSystem,
35-
found_packages: Set[FoundPackage],
33+
found_packages: set[FoundPackage],
3634
*,
3735
include_external_packages: bool,
3836
exclude_type_checking_imports: bool = False,
39-
cache_dir: Optional[str] = None,
37+
cache_dir: str | None = None,
4038
) -> "Cache":
4139
cache = cls(
4240
file_system=file_system,
@@ -47,15 +45,15 @@ def setup(
4745
)
4846
return cache
4947

50-
def read_imports(self, module_file: ModuleFile) -> Set[DirectImport]:
48+
def read_imports(self, module_file: ModuleFile) -> set[DirectImport]:
5149
raise NotImplementedError
5250

5351
def write(
5452
self,
55-
imports_by_module: Dict[Module, Set[DirectImport]],
53+
imports_by_module: dict[Module, set[DirectImport]],
5654
) -> None:
5755
raise NotImplementedError
5856

5957
@classmethod
60-
def cache_dir_or_default(cls, cache_dir: Optional[str]) -> str:
58+
def cache_dir_or_default(cls, cache_dir: str | None) -> str:
6159
raise NotImplementedError

src/grimp/application/ports/filesystem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
import abc
3-
from typing import Iterator, List, Tuple
3+
from collections.abc import Iterator
44
from typing import Protocol
55

66

@@ -28,7 +28,7 @@ def dirname(self, filename: str) -> str:
2828
raise NotImplementedError
2929

3030
@abc.abstractmethod
31-
def walk(self, directory_name: str) -> Iterator[Tuple[str, List[str], List[str]]]:
31+
def walk(self, directory_name: str) -> Iterator[tuple[str, list[str], list[str]]]:
3232
"""
3333
Given a directory, walk the file system recursively.
3434
@@ -42,7 +42,7 @@ def join(self, *components: str) -> str:
4242
raise NotImplementedError
4343

4444
@abc.abstractmethod
45-
def split(self, file_name: str) -> Tuple[str, str]:
45+
def split(self, file_name: str) -> tuple[str, str]:
4646
"""
4747
Split the pathname path into a pair, (head, tail) where tail is the last pathname component
4848
and head is everything leading up to that. The tail part will never contain a slash;
@@ -105,7 +105,7 @@ class BasicFileSystem(Protocol):
105105

106106
def join(self, *components: str) -> str: ...
107107

108-
def split(self, file_name: str) -> Tuple[str, str]: ...
108+
def split(self, file_name: str) -> tuple[str, str]: ...
109109

110110
def read(self, file_name: str) -> str: ...
111111

0 commit comments

Comments
 (0)