Skip to content

Commit a555553

Browse files
committed
Fix linting issues
1 parent 1df5d1c commit a555553

24 files changed

Lines changed: 41 additions & 168 deletions

docs_src/how_to_guides/migrating_from_scripts_to_pytask_5.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from pathlib import Path
22
from typing import Annotated
3-
from typing import Optional
43

54
import pandas as pd
65

76
from pytask import Product
87

98

109
def task_merge_data(
11-
paths_to_input_data: Optional[dict[str, Path]] = None,
10+
paths_to_input_data: dict[str, Path] | None = None,
1211
path_to_merged_data: Annotated[Path, Product] = Path("merged_data.pkl"),
1312
) -> None:
1413
if paths_to_input_data is None:

docs_src/how_to_guides/writing_custom_nodes_example_3_py38.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pickle
33
from pathlib import Path
44
from typing import Any
5-
from typing import Optional
65

76
from pytask import hash_value
87

@@ -24,8 +23,8 @@ class PickleNode:
2423
def __init__(
2524
self,
2625
name: str = "",
27-
path: Optional[Path] = None,
28-
attributes: Optional[dict[Any, Any]] = None,
26+
path: Path | None = None,
27+
attributes: dict[Any, Any] | None = None,
2928
) -> None:
3029
self.name = name
3130
self.path = path
@@ -45,7 +44,7 @@ def from_path(cls, path: Path) -> "PickleNode":
4544
raise ValueError(msg)
4645
return cls(name=path.as_posix(), path=path)
4746

48-
def state(self) -> Optional[str]:
47+
def state(self) -> str | None:
4948
"""Return the modification timestamp as the state."""
5049
if self.path.exists():
5150
return str(self.path.stat().st_mtime)

src/_pytask/_inspect.py

Lines changed: 1 addition & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,6 @@
11
from __future__ import annotations
22

3-
import functools
4-
import sys
5-
import types
6-
from typing import TYPE_CHECKING
7-
from typing import Any
8-
from typing import Callable
9-
10-
if TYPE_CHECKING:
11-
from collections.abc import Mapping
12-
133
__all__ = ["get_annotations"]
144

155

16-
if sys.version_info >= (3, 10): # pragma: no cover
17-
from inspect import get_annotations
18-
else: # pragma: no cover
19-
20-
def get_annotations( # noqa: C901, PLR0912, PLR0915
21-
obj: Callable[..., object] | type[Any] | types.ModuleType,
22-
*,
23-
globals: Mapping[str, Any] | None = None, # noqa: A002
24-
locals: Mapping[str, Any] | None = None, # noqa: A002
25-
eval_str: bool = False,
26-
) -> dict[str, Any]:
27-
"""Compute the annotations dict for an object.
28-
29-
obj may be a callable, class, or module.
30-
Passing in an object of any other type raises TypeError.
31-
32-
Returns a dict. get_annotations() returns a new dict every time
33-
it's called; calling it twice on the same object will return two
34-
different but equivalent dicts.
35-
36-
This function handles several details for you:
37-
38-
* If eval_str is true, values of type str will
39-
be un-stringized using eval(). This is intended
40-
for use with stringized annotations
41-
("from __future__ import annotations").
42-
* If obj doesn't have an annotations dict, returns an
43-
empty dict. (Functions and methods always have an
44-
annotations dict; classes, modules, and other types of
45-
callables may not.)
46-
* Ignores inherited annotations on classes. If a class
47-
doesn't have its own annotations dict, returns an empty dict.
48-
* All accesses to object members and dict values are done
49-
using getattr() and dict.get() for safety.
50-
* Always, always, always returns a freshly-created dict.
51-
52-
eval_str controls whether or not values of type str are replaced
53-
with the result of calling eval() on those values:
54-
55-
* If eval_str is true, eval() is called on values of type str.
56-
* If eval_str is false (the default), values of type str are unchanged.
57-
58-
globals and locals are passed in to eval(); see the documentation
59-
for eval() for more information. If either globals or locals is
60-
None, this function may replace that value with a context-specific
61-
default, contingent on type(obj):
62-
63-
* If obj is a module, globals defaults to obj.__dict__.
64-
* If obj is a class, globals defaults to
65-
sys.modules[obj.__module__].__dict__ and locals
66-
defaults to the obj class namespace.
67-
* If obj is a callable, globals defaults to obj.__globals__,
68-
although if obj is a wrapped function (using
69-
functools.update_wrapper()) it is first unwrapped.
70-
"""
71-
if isinstance(obj, type):
72-
# class
73-
obj_dict = getattr(obj, "__dict__", None)
74-
if obj_dict and hasattr(obj_dict, "get"):
75-
ann = obj_dict.get("__annotations__", None)
76-
if isinstance(ann, types.GetSetDescriptorType):
77-
ann = None
78-
else:
79-
ann = None
80-
81-
obj_globals = None
82-
module_name = getattr(obj, "__module__", None)
83-
if module_name:
84-
module = sys.modules.get(module_name, None)
85-
if module:
86-
obj_globals = getattr(module, "__dict__", None)
87-
obj_locals = dict(vars(obj))
88-
unwrap = obj
89-
elif isinstance(obj, types.ModuleType):
90-
# module
91-
ann = getattr(obj, "__annotations__", None)
92-
obj_globals = obj.__dict__
93-
obj_locals = None
94-
unwrap = None
95-
elif callable(obj):
96-
# this includes types.Function, types.BuiltinFunctionType,
97-
# types.BuiltinMethodType, functools.partial, functools.singledispatch,
98-
# "class funclike" from Lib/test/test_inspect... on and on it goes.
99-
ann = getattr(obj, "__annotations__", None)
100-
obj_globals = getattr(obj, "__globals__", None)
101-
obj_locals = None
102-
unwrap = obj
103-
else:
104-
msg = f"{obj!r} is not a module, class, or callable."
105-
raise TypeError(msg)
106-
107-
if ann is None:
108-
return {}
109-
110-
if not isinstance(ann, dict):
111-
msg = f"{obj!r}.__annotations__ is neither a dict nor None"
112-
raise ValueError(msg) # noqa: TRY004
113-
114-
if not ann:
115-
return {}
116-
117-
if not eval_str:
118-
return dict(ann)
119-
120-
if unwrap is not None:
121-
while True:
122-
if hasattr(unwrap, "__wrapped__"):
123-
unwrap = unwrap.__wrapped__
124-
continue
125-
if isinstance(unwrap, functools.partial):
126-
unwrap = unwrap.func
127-
continue
128-
break
129-
if hasattr(unwrap, "__globals__"):
130-
obj_globals = unwrap.__globals__
131-
132-
if globals is None:
133-
globals = obj_globals # noqa: A001
134-
if locals is None:
135-
locals = obj_locals # noqa: A001
136-
137-
eval_func = eval
138-
return {
139-
key: value
140-
if not isinstance(value, str)
141-
else eval_func(value, globals, locals)
142-
for key, value in ann.items()
143-
}
6+
from inspect import get_annotations

src/_pytask/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99
from typing import TYPE_CHECKING
1010
from typing import Any
11-
from typing import Callable
1211
from typing import Literal
1312

1413
import click
@@ -35,6 +34,7 @@
3534
from _pytask.traceback import Traceback
3635

3736
if TYPE_CHECKING:
37+
from collections.abc import Callable
3838
from collections.abc import Iterable
3939
from typing import NoReturn
4040

src/_pytask/cache.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import hashlib
77
import inspect
88
from inspect import FullArgSpec
9+
from typing import TYPE_CHECKING
910
from typing import Any
10-
from typing import Callable
1111

1212
from attrs import define
1313
from attrs import field
1414

1515
from _pytask._hashlib import hash_value
1616

17+
if TYPE_CHECKING:
18+
from collections.abc import Callable
19+
1720

1821
@define
1922
class CacheInfo:

src/_pytask/coiled_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
34
from typing import Any
4-
from typing import Callable
55

66
from attrs import define
77

8+
if TYPE_CHECKING:
9+
from collections.abc import Callable
10+
811
try:
912
from coiled.function import Function
1013
except ImportError:

src/_pytask/collect_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import TYPE_CHECKING
77
from typing import Annotated
88
from typing import Any
9-
from typing import Callable
109
from typing import get_origin
1110

1211
import attrs
@@ -25,6 +24,7 @@
2524
from _pytask.typing import no_default
2625

2726
if TYPE_CHECKING:
27+
from collections.abc import Callable
2828
from pathlib import Path
2929

3030
from _pytask.session import Session

src/_pytask/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99
from typing import TYPE_CHECKING
1010
from typing import Any
11-
from typing import Callable
1211

1312
from rich.console import Console
1413
from rich.console import RenderableType
@@ -29,6 +28,7 @@
2928
from _pytask.path import shorten_path
3029

3130
if TYPE_CHECKING:
31+
from collections.abc import Callable
3232
from collections.abc import Iterable
3333
from collections.abc import Sequence
3434
from enum import Enum

src/_pytask/dag_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def _shorten_node_labels(dag: nx.DiGraph, paths: list[Path]) -> nx.DiGraph:
216216
node_names = dag.nodes
217217
short_names = reduce_names_of_multiple_nodes(node_names, dag, paths)
218218
short_names = [i.plain if isinstance(i, Text) else i for i in short_names] # type: ignore[attr-defined]
219-
old_to_new = dict(zip(node_names, short_names))
219+
old_to_new = dict(zip(node_names, short_names, strict=False))
220220
return nx.relabel_nodes(dag, old_to_new)
221221

222222

src/_pytask/execute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def pytask_execute_task(session: Session, task: PTask) -> bool:
287287

288288
nodes = tree_leaves(task.produces["return"])
289289
values = structure_return.flatten_up_to(out)
290-
for node, value in zip(nodes, values):
290+
for node, value in zip(nodes, values, strict=False):
291291
if not isinstance(node, PProvisionalNode):
292292
node.save(value)
293293

0 commit comments

Comments
 (0)