-
Notifications
You must be signed in to change notification settings - Fork 42
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
support for ignoring args #115
Conversation
Hi @rainyl, Thank you for contributing! We appreciate you adding Prior to merging, we have a few concrete recommendations and questions. For subscriptable types in a TapIgnore, the IDE does not seem to know that Ideally, there'd be a test case then ensures that TapIgnore typed arguments cannot be provided on the command line. We're not familiar with the The current code seems to fail a test: We'll do our best to help out, but would love further contributions. Thank you again! Best, |
Hi, @martinjm97 Well, after working for some time, I used some hacking techniques to implement it on python version >=3.9, for 3.8 and former, the best solution seems to be using ClassVar directly. TL;DR, regard TapIgnore as an alias to ClassVar, but with a name of "TapIgnore" TapIgnore = ClassVar
TapIgnore._name = "TapIgnore" Specifically, a user-defined type Finally, I think the best implementation is that: Also, If you have better solutions, please let me know :) |
As for this, I find it had been solved. For example, for the following code from tap import Tap, TapIgnore
class MyTapWithIgnore(Tap):
arg_to_ignore_by_comment: str # tap: ignore
attr_to_ignore: TapIgnore[str]
hello: str = "hello"
args = MyTapWithIgnore().parse_args([
"--hello", "hello world",
# "--attr_to_ignore", "ignored",
# "--arg_to_ignore_by_comment", "ignored by comment"
])
print(args) and the output: (venv38) > python .\test.py
usage: test.py [--hello HELLO] [-h]
test.py: error: unrecognized arguments: --attr_to_ignore ignored --arg_to_ignore_by_comment ignored by comment
(venv38) > python .\test.py
usage: test.py [--hello HELLO] [-h]
test.py: error: unrecognized arguments: --attr_to_ignore ignored
(venv38) > python .\test.py
{'hello': 'hello world'} |
Well, the codes you provided above won't work in VSCode. T = TypeVar('T')
TapIgnore: TypeAlias = ClassVar[T]
TapIgnore._name = 'TapIgnore' but this worked: TapIgnore = ClassVar
TapIgnore._name = "TapIgnore" However, this solution seems won't work in pycharm, which has it's own type checking tool. As for TapIgnore._name="TapIgnore", It is because that, the name of Finally, I suggest that just use ClassVar to indicate ignored variables, the tricks above may not work in every type checking tool like pylance, mypy, and pycharm's, but they all support ClassVar. |
Hi @rainyl, Thank you again for the follow up! What an interesting problem. We weren't able to exactly reproduce the issue you observed, but we had a similar problem with VSCode until we installed mypy. For Python 3.10.0, we used your test case above and looked at results in PyCharm and VSCode with T = TypeVar('T')
TapIgnore: TypeAlias = ClassVar[T]
TapIgnore._name = 'TapIgnore' VSCode version 1.82.1 Pycharm version 2023.1.1 (Community Edition) Let us know if installing mypy works for you as well. Thank you again, |
Just do the same thing as #75 and #92 , but meet the format mentioned at #92 (comment)
for now, args can be ignored as follow:
and the output:
By the way, some codes were borrowed from #92 and the official typing library.