Skip to content
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

fix: support future annotations #621

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: pylint
run: |
echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json"
pipx run nox -s pylint
pipx run --python python nox -s pylint

tests:
name: Tests on 🐍 ${{ matrix.python-version }} ${{ matrix.os }}
Expand Down
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ repos:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
- id: python-no-eval
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
Expand Down
19 changes: 16 additions & 3 deletions plumbum/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,12 @@ def _validate_args(self, swfuncs, tailargs):
)

m = inspect.getfullargspec(self.main)

if sys.version_info < (3, 10):
sig = inspect.signature(self.main)
else:
sig = inspect.signature(self.main, eval_str=True)

max_args = sys.maxsize if m.varargs else len(m.args) - 1
min_args = len(m.args) - 1 - (len(m.defaults) if m.defaults else 0)
if len(tailargs) < min_args:
Expand Down Expand Up @@ -530,17 +536,24 @@ def _validate_args(self, swfuncs, tailargs):
m.varargs,
)

elif hasattr(m, "annotations"):
elif hasattr(m, "annotations") and m.annotations:
args_names = list(m.args[1:])
positional = [None] * len(args_names)
varargs = None

# All args are positional, so convert kargs to positional
for item in m.annotations:
annotation = (
sig.parameters[item].annotation
if item != "return"
else sig.return_annotation
)
if sys.version_info < (3, 10) and isinstance(annotation, str):
annotation = eval(annotation)
if item == m.varargs:
varargs = m.annotations[item]
varargs = annotation
elif item != "return":
positional[args_names.index(item)] = m.annotations[item]
positional[args_names.index(item)] = annotation

tailargs = self._positional_validate(
tailargs, positional, varargs, m.args[1:], m.varargs
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ messages_control.disable = [
"unidiomatic-typecheck", # TODO: might be able to remove
"unnecessary-lambda-assignment", # TODO: 4 instances
"unused-import", # identical to flake8 but has typing false positives
"eval-used", # Needed for Python <3.10 annotations
]
2 changes: 1 addition & 1 deletion tests/test_3_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_prog(self, capsys):


class Main4Validator(cli.Application):
def main(self, myint: int, myint2: int, *mylist: int) -> None:
def main(self, myint: int, myint2: int, *mylist: "int") -> None:
print(myint, myint2, mylist)


Expand Down