Skip to content

Commit

Permalink
Fix #19: add CLI options to %ruff and %pytype (#20)
Browse files Browse the repository at this point in the history
This is to avoid requiring `pyproject.toml` in cloud environments.

* Add --select and --ignore options to %ruff, with defaults from pyproject.toml
* Add --disable option to %pytype with default from pyproject.toml
* Update documentation

---------

Co-authored-by: Michel Wermelinger <[email protected]>
  • Loading branch information
densnow and mwermelinger committed Mar 14, 2024
1 parent afc6a3a commit c11de47
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 130 deletions.
16 changes: 4 additions & 12 deletions docs/getting-started-m269/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -873,21 +873,13 @@ <h2 id="install-software-optional">Install software (optional)</h2>
</ul>
</li>
<li>
<p>To install the software, enter <code>pip install algoesup ruff allowed</code> in the PowerShell / terminal.</p>
<p>To install the software, enter <code>pip install algoesup ruff</code> in the PowerShell / terminal.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The command installs a version of <code>allowed</code> that can be used from
within the notebook, to check the code cell by cell.
Do <em>not</em> delete the <code>allowed.py</code> and <code>m269.json</code> files in your M269 folder.
You will continue to need them to check your TMAs before submission.</p>
<p>You also need the most recent version of <code>allowed</code>, which is available
from the Resources tab of the M269 website.</p>
</div>
</li>
<li>
<p>The software needs a configuration file named <code>pyproject.toml</code>.
Right-click on <a href="https://github.com/dsa-ou/algoesup/raw/main/src/docs/essays/pyproject.toml">this link</a>
and select &lsquo;Download / Save file as&hellip;&rsquo; to save the file to your M269 folder.
Do <em>not</em> change its name.</p>
</li>
</ol>
<h2 id="copy-template">Copy template</h2>
<p>We have created templates to serve as starting points for your essay.</p>
Expand All @@ -896,7 +888,7 @@ <h2 id="copy-template">Copy template</h2>
<li>Choose a template and <em>right</em>-click on the corresponding download button.</li>
<li>Select &lsquo;Download / Save file as&hellip;&rsquo; and save the template to your M269 folder,
with a filename that includes your name (e.g. <code>essay-Michael.ipynb</code>)
or indicates the topic (e.g. <code>balanced-brackets.ipynb</code>).
or that indicates the topic (e.g. <code>balanced-brackets.ipynb</code>).
If you prefer, you can rename the file later, when you settled on a topic.</li>
</ol>
<h2 id="write-the-essay">Write the essay</h2>
Expand Down
78 changes: 33 additions & 45 deletions docs/library/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1595,21 +1595,18 @@ <h3 id="algoesup.magics.allowed" class="doc doc-heading">

<div class="doc doc-contents ">

<p>Activate/deactivate the <code>allowed</code> linter.</p>
<p>This ipython magic command controls the activation state of the <code>allowed</code> linter
within the ipython environment. It can be toggled on or off, or queried for its
current state.</p>



<p><strong>Examples:</strong></p>
<div class="highlight"><pre><span></span><code>%allowed on
pytype was activated
%allowed off
pytype was deactivated
%allowed
pytype is inactive
</code></pre></div>
<p>Activate/deactivate the <a href="https://dsa-ou.github.io/allowed">allowed linter</a>.</p>
<p>When active, the linter checks each code cell that is executed for any
Python constructs that are not listed in the given configuration file.</p>
<ul>
<li><code>%allowed --config ... on</code> activates the linter with the given configuration,
which must be <code>m269.json</code>, <code>tm112.json</code> or
<a href="https://dsa-ou.github.io/allowed/docs/configuration.html">one you defined</a></li>
<li><code>%allowed on</code> is equal to <code>%allowed --config m269.json on</code></li>
<li><code>%allowed off</code> deactivates the linter</li>
<li><code>%allowed</code> shows the current status of the linter</li>
<li><code>%allowed?</code> shows this documentation and the command&rsquo;s options</li>
</ul>

</div>

Expand All @@ -1630,21 +1627,16 @@ <h3 id="algoesup.magics.pytype" class="doc doc-heading">

<div class="doc doc-contents ">

<p>Activate/deactivate the <code>pytype</code> linter.</p>
<p>This ipython magic command controls the activation state of the <code>pytype</code> linter within
the ipython environment. It can be toggled on or off, or queried for its
current state.</p>



<p><strong>Examples:</strong></p>
<div class="highlight"><pre><span></span><code>%pytype on
pytype was activated
%pytype off
pytype was deactivated
%pytype
pytype is inactive
</code></pre></div>
<p>Activate/deactivate the <a href="https://google.github.io/pytype">pytype linter</a>.</p>
<p>When active, the linter checks each code cell that is executed for type errors.</p>
<ul>
<li><code>%pytype --disable ... on</code> activates the linter but does not check the given errors
(see the <a href="https://google.github.io/pytype/errors.html">list of errors</a>)</li>
<li><code>%pytype on</code> is equal to <code>%pytype --disable name-error,import-error on</code></li>
<li><code>%pytype off</code> deactivates the linter</li>
<li><code>%pytype</code> shows the current status of the linter</li>
<li><code>%pytype?</code> shows this documentation and the command&rsquo;s options</li>
</ul>

</div>

Expand All @@ -1665,21 +1657,17 @@ <h3 id="algoesup.magics.ruff" class="doc doc-heading">

<div class="doc doc-contents ">

<p>Activate/deactivate the <code>ruff</code> linter.</p>
<p>This ipython magic command controls the activation state of the <code>ruff</code> linter within
the ipython environment. It can be toggled on or off, or queried for its
current state.</p>



<p><strong>Examples:</strong></p>
<div class="highlight"><pre><span></span><code>%ruff on
ruff was activated
%ruff off
ruff was deactivated
%ruff
ruff is inactive
</code></pre></div>
<p>Activate/deactivate the <a href="https://docs.astral.sh/ruff">Ruff linter</a>.</p>
<p>When active, the linter checks each code cell that is executed
against the selected code style rules.</p>
<ul>
<li><code>%ruff --select ... --ignore ... on</code> activates the linter with the given rules
(see <a href="https://docs.astral.sh/ruff/rules">the list of rules</a>)</li>
<li><code>%ruff on</code> is equal to <code>%ruff --select A,B,C90,D,E,W,F,N,PL --ignore D100,W292,F401,F821,D203,D213,D415 on</code></li>
<li><code>%ruff off</code> deactivates the linter</li>
<li><code>%ruff</code> shows the current status of the linter</li>
<li><code>%ruff?</code> shows this documentation and the command&rsquo;s options</li>
</ul>

</div>

Expand Down
2 changes: 1 addition & 1 deletion docs/search/search_index.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,87 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://dsa-ou.github.io/algoesup/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/deepnote-background/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/deepnote-how-to/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/deepnote-reference/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/deepnote/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/example-essays/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/feedback/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/getting-started-m269/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/getting-started/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/library/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/writing/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/example-1-to-n/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/example-jewels/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/example-two-sum-2/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/example-two-sum-3/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/template-data-structures/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://dsa-ou.github.io/algoesup/essays/template-intro-programming/</loc>
<lastmod>2024-02-29</lastmod>
<lastmod>2024-03-14</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
Binary file modified docs/sitemap.xml.gz
Binary file not shown.
96 changes: 52 additions & 44 deletions src/algoesup/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,27 @@ def process_status(name: str, status: str) -> None:
nargs="?",
default=None,
)
@argument(
"-d",
"--disable",
default="name-error,import-error",
help="Comma or space-separated list of error names to ignore",
)
@register_line_magic
def pytype(line: str) -> None:
"""Activate/deactivate the `pytype` linter.
This ipython magic command controls the activation state of the `pytype` linter within
the ipython environment. It can be toggled on or off, or queried for its
current state.
"""Activate/deactivate the [pytype linter](https://google.github.io/pytype).
Examples:
```
%pytype on
pytype was activated
%pytype off
pytype was deactivated
%pytype
pytype is inactive
```
When active, the linter checks each code cell that is executed for type errors.
- `%pytype --disable ... on` activates the linter but does not check the given errors
(see the [list of errors](https://google.github.io/pytype/errors.html))
- `%pytype on` is equal to `%pytype --disable name-error,import-error on`
- `%pytype off` deactivates the linter
- `%pytype` shows the current status of the linter
- `%pytype?` shows this documentation and the command's options
"""
args = parse_argstring(pytype, line)
checkers["pytype"][0] = ["pytype", "--disable", args.disable]
process_status("pytype", args.status)


Expand All @@ -128,21 +130,18 @@ def pytype(line: str) -> None:
)
@register_line_magic
def allowed(line: str) -> None:
"""Activate/deactivate the `allowed` linter.
"""Activate/deactivate the [allowed linter](https://dsa-ou.github.io/allowed).
This ipython magic command controls the activation state of the `allowed` linter
within the ipython environment. It can be toggled on or off, or queried for its
current state.
Examples:
```
%allowed on
pytype was activated
%allowed off
pytype was deactivated
%allowed
pytype is inactive
```
When active, the linter checks each code cell that is executed for any
Python constructs that are not listed in the given configuration file.
- `%allowed --config ... on` activates the linter with the given configuration,
which must be `m269.json`, `tm112.json` or
[one you defined](https://dsa-ou.github.io/allowed/docs/configuration.html)
- `%allowed on` is equal to `%allowed --config m269.json on`
- `%allowed off` deactivates the linter
- `%allowed` shows the current status of the linter
- `%allowed?` shows this documentation and the command's options
"""
args = parse_argstring(allowed, line)
if args.config:
Expand All @@ -159,25 +158,35 @@ def allowed(line: str) -> None:
nargs="?",
default=None,
)
@argument(
"--select",
help="Comma-separated list of rule codes to enable",
type=str,
default="A,B,C90,D,E,W,F,N,PL",
)
@argument(
"--ignore",
help="Comma-separated list of rule codes to ignore",
type=str,
default="D100,W292,F401,F821,D203,D213,D415",
)
@register_line_magic
def ruff(line: str) -> None:
"""Activate/deactivate the `ruff` linter.
"""Activate/deactivate the [Ruff linter](https://docs.astral.sh/ruff).
When active, the linter checks each code cell that is executed
against the selected code style rules.
This ipython magic command controls the activation state of the `ruff` linter within
the ipython environment. It can be toggled on or off, or queried for its
current state.
Examples:
```
%ruff on
ruff was activated
%ruff off
ruff was deactivated
%ruff
ruff is inactive
```
- `%ruff --select ... --ignore ... on` activates the linter with the given rules
(see [the list of rules](https://docs.astral.sh/ruff/rules))
- `%ruff on` is equal to `%ruff --select A,B,C90,D,E,W,F,N,PL --ignore D100,W292,F401,F821,D203,D213,D415 on`
- `%ruff off` deactivates the linter
- `%ruff` shows the current status of the linter
- `%ruff?` shows this documentation and the command's options
"""
args = parse_argstring(ruff, line)
base = ["ruff", "check", "--output-format", "json"]
checkers["ruff"][0] = base + ["--select", args.select, "--ignore", args.ignore]
process_status("ruff", args.status)


Expand Down Expand Up @@ -228,8 +237,7 @@ def run_checkers(result) -> None:
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as temp:
# transform IPython to pure Python to avoid linters reporting syntax errors
temp.write(TransformerManager().transform_cell(result.info.raw_cell))
# Backslash causes esc sequence error from standard Windows file paths,
# but Windows accepts both "\" and "/" as separators.
# Handle Windows file paths
temp_name = temp.name.replace("\\", "/")
for checker in active:
command, display = checkers[checker]
Expand Down
Loading

0 comments on commit c11de47

Please sign in to comment.