-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize document_cli_flags.py working on issue #81 #103
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,25 @@ | ||
#!/usr/bin/env python | ||
"""Fill in the CLI reference in euporie's documentation.""" | ||
|
||
from __future__ import annotations | ||
|
||
import subprocess | ||
import sys | ||
from textwrap import dedent, indent | ||
from typing import TYPE_CHECKING, cast | ||
from typing import TYPE_CHECKING, Callable | ||
|
||
if TYPE_CHECKING: | ||
import argparse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you remove With your changes, this script actually fails to run with a Traceback (most recent call last):
File "/home/josiah/data/projects/euporie/scripts/document_cli_flags.py", line 17, in <module>
def format_action(action: argparse.Action) -> str:
^^^^^^^^
NameError: name 'argparse' is not define I've made the decision to use future imports throughout this codebase, and do not want to stop using it for this one script. I decided to use it because I use modern type annotation syntax for type checking (e.g. |
||
from typing import Callable | ||
|
||
if sys.version_info[0] >= 3 and sys.version_info[1] >= 10: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't object to this change, I guess it is a little more readable. |
||
if sys.version_info[:2] >= (3, 10): | ||
from importlib.metadata import entry_points | ||
else: | ||
from importlib_metadata import entry_points | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using ruff to automatically format all Python scripts in this project. Ruff follows black's formatting style, which requires two new-lines between top level code blocks. Also I don't think it helps improve readability, so I don't want to remove of the double newlines. |
||
def format_action(action: argparse.Action) -> str: | ||
"""Format an action as RST.""" | ||
s = "" | ||
type_ = "" | ||
if action.type and action.type != bool: | ||
action.type = cast("Callable", action.type) | ||
type_ = f"<{action.type.__name__}>" # typing: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use
The reason for this is that the argparse action However, here I know that the action type is a callable, as I have defined them, hence the need for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
type_ = f"<{action.type.__name__}>" | ||
if action.choices: | ||
type_ = f"{{{','.join(map(str, action.choices))}}}" | ||
|
||
|
@@ -38,18 +33,12 @@ def format_action(action: argparse.Action) -> str: | |
""" | ||
return s | ||
|
||
|
||
def format_parser( | ||
title: str, parser: argparse.ArgumentParser, description: str = "" | ||
) -> str: | ||
"""Format a parser's arguments as RST.""" | ||
s = "" | ||
|
||
# s = "\n" | ||
# s += ("*" * len(title)) + "\n" + title + "\n" + ("*" * len(title)) + "\n\n" | ||
# s += description or dedent(parser.description or "").strip() | ||
# s += "\n\n" | ||
|
||
s += "\nUsage\n=====\n\n" | ||
s += ".. code-block:: console\n\n" | ||
usage = parser.format_usage() | ||
|
@@ -77,7 +66,6 @@ def format_parser( | |
|
||
return s | ||
|
||
|
||
if __name__ == "__main__": | ||
for script in entry_points(group="console_scripts"): | ||
if script.module.split(".")[0] == "euporie": | ||
|
@@ -100,4 +88,4 @@ def format_parser( | |
break | ||
break | ||
else: | ||
subprocess.call([sys.executable, __file__, script.name]) # S603 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've actually configured ruff to ignore |
||
subprocess.call([sys.executable, __file__, script.name]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've removed the
cast
call whereCallable
is used, so this import would no longer be necessary.