Skip to content

Commit 01d7d3f

Browse files
authored
Invoke pytask through the Python interpreter. (#51)
1 parent 21ba23a commit 01d7d3f

File tree

9 files changed

+46
-10
lines changed

9 files changed

+46
-10
lines changed

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ repos:
1010
- id: debug-statements
1111
exclude: (debugging\.py|build\.py|clean\.py|mark/__init__\.py|collect_command\.py)
1212
- id: end-of-file-fixer
13+
- repo: https://github.com/pre-commit/pygrep-hooks
14+
rev: v1.7.0 # Use the ref you want to point at
15+
hooks:
16+
- id: python-check-blanket-noqa
17+
- id: python-check-mock-methods
18+
- id: python-no-eval
19+
exclude: expression.py
20+
- id: python-no-log-warn
21+
- id: python-use-type-annotations
22+
- id: rst-backticks
23+
- id: rst-directive-colons
24+
- id: rst-inline-touching-normal
25+
- id: text-unicode-replacement-char
1326
- repo: https://github.com/asottile/pyupgrade
1427
rev: v2.7.4
1528
hooks:

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ all releases are available on `Anaconda.org <https://anaconda.org/pytask/pytask>
1313
:gh:`44`.
1414
- :gh:`47` reduce node names in error messages while resolving dependencies.
1515
- :gh:`50` implements correct usage of singular and plural in collection logs.
16+
- :gh:`51` allows to invoke pytask through the Python interpreter with ``python -m
17+
pytask`` which will add the current path to ``sys.path``.
1618

1719

1820
0.0.10 - 2020-11-18

docs/tutorials/how_to_invoke_pytask.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ There are two entry-points to invoke pytask.
1919
$ pytask --version
2020
$ pytask -h | --help
2121
22-
2. Invoke pytask programmatically with
22+
2. Invoke pytask via the Python interpreter which will add the current path to the
23+
``sys.path``.
24+
25+
.. code-block:: console
26+
27+
python -m pytask /some/task/dir
28+
29+
3. Invoke pytask programmatically with
2330

2431
.. code-block:: python
2532

src/_pytask/capture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class CaptureManager:
783783

784784
def __init__(self, method: "_CaptureMethod") -> None:
785785
self._method = method
786-
self._global_capturing = None # type: Optional[MultiCapture[str]]
786+
self._global_capturing: Optional[MultiCapture[str]] = None
787787

788788
def __repr__(self) -> str:
789789
return ("<CaptureManager _method={!r} _global_capturing={!r}>").format(

src/_pytask/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def pytask_log_session_header(session):
3333

3434
def _format_plugin_names_and_versions(plugininfo) -> List[str]:
3535
"""Format name and version of loaded plugins."""
36-
values = [] # type: List[str]
36+
values: List[str] = []
3737
for _, dist in plugininfo:
3838
# Gets us name and version!
3939
name = f"{dist.project_name}-{dist.version}"

src/_pytask/mark/expression.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def reject(self, expected: Sequence[TokenType]):
134134

135135
def expression(s: Scanner) -> ast.Expression:
136136
if s.accept(TokenType.EOF):
137-
ret = ast.NameConstant(False) # type: ast.expr
137+
ret: ast.expr = ast.NameConstant(False)
138138
else:
139139
ret = expr(s)
140140
s.accept(TokenType.EOF, reject=True)
@@ -209,11 +209,11 @@ def compile_(cls, input_: str) -> "Expression":
209209
210210
"""
211211
astexpr = expression(Scanner(input_))
212-
code = compile(
212+
code: types.CodeType = compile(
213213
astexpr,
214214
filename="<pytest match expression>",
215215
mode="eval",
216-
) # type: types.CodeType
216+
)
217217
return cls(code)
218218

219219
def evaluate(self, matcher: Callable[[str], bool]) -> bool:
@@ -231,7 +231,5 @@ def evaluate(self, matcher: Callable[[str], bool]) -> bool:
231231
Whether the expression matches or not.
232232
233233
"""
234-
ret = eval(
235-
self.code, {"__builtins__": {}}, MatcherAdapter(matcher)
236-
) # type: bool
234+
ret: bool = eval(self.code, {"__builtins__": {}}, MatcherAdapter(matcher))
237235
return ret

src/_pytask/mark/structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def combined_with(self, other: "Mark") -> "Mark":
6969
assert self.name == other.name
7070

7171
# Remember source of ids with parametrize Marks.
72-
param_ids_from = None # type: Optional[Mark]
72+
param_ids_from: Optional[Mark] = None
7373
if self.name == "parametrize":
7474
if other._has_param_ids():
7575
param_ids_from = other

src/pytask/__main__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""The pytask entry-point."""
2+
import sys
3+
4+
import pytask
5+
6+
7+
if __name__ == "__main__":
8+
sys.exit(pytask.cli())

tests/test_execute.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
import textwrap
23

34
import pytest
@@ -6,6 +7,13 @@
67
from pytask import main
78

89

10+
@pytest.mark.end_to_end
11+
def test_python_m_pytask(tmp_path):
12+
source = "def task_dummy(): pass"
13+
tmp_path.joinpath("task_dummy.py").write_text(source)
14+
subprocess.run(["python", "-m", "pytask", tmp_path.as_posix()], check=True)
15+
16+
917
@pytest.mark.end_to_end
1018
def test_task_did_not_produce_node(tmp_path):
1119
source = """

0 commit comments

Comments
 (0)