Skip to content

Fix/type action restriction #687

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

Merged
merged 6 commits into from
Mar 5, 2025

Conversation

PabloLION
Copy link
Contributor

@PabloLION PabloLION commented Mar 3, 2025

What does this PR do?

Fix #685
Now in add_argument, passing action=None behaves like call without argument action.

parser.add_argument(
    "--string",
    type=str,
    action=None,
    default="default",
    help="example string",
)

Before submitting

  • Did you read the contributing guideline?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

@PabloLION
Copy link
Contributor Author

Additional info

When creating remote branch, the git hook fired and I got this

mypy................................................................................Failed
- hook id: mypy
- duration: 0.98s
- exit code: 1

jsonargparse/_stubs_resolver.py:51: error: No overload variant of "Assign" matches argument types "list[Name | Attribute | Subscript]", "expr | None", "list[Never]", "int", "int"  [call-overload]
jsonargparse/_stubs_resolver.py:51: note: Possible overload variants:
jsonargparse/_stubs_resolver.py:51: note:     def Assign(self, targets: list[expr], value: expr, type_comment: str | None = ..., *, lineno: int = ..., col_offset: int = ..., end_lineno: int | None = ..., end_col_offset: int | None = ...) -> Assign
jsonargparse/_stubs_resolver.py:51: note:     def Assign(self, targets: list[expr] = ..., *, value: expr, type_comment: str | None = ..., lineno: int = ..., col_offset: int = ..., end_lineno: int | None = ..., end_col_offset: int | None = ...) -> Assign
jsonargparse/_stubs_resolver.py:53: error: Unused "type: ignore" comment  [unused-ignore]
jsonargparse/_stubs_resolver.py:54: error: Unused "type: ignore" comment  [unused-ignore]
Found 3 errors in 1 file (checked 3 source files)

Since I didn't edit these files, I used --no-verify.

Also, I didn't have mypy/ruff installed (which made. Maybe we can consider adding them to the contributing guideline? I saw them in pyproject.toml but I could format my code :-( without them.

@PabloLION
Copy link
Contributor Author

not sure where to put the test:

  • test_typehints (I added here: the edited file is _typehints.py, and the feature is also about a valid type with a None action)
  • test_actions (the feature is about action)
  • test_core (the feature is about add_argument)
  • test_formatters (do we need to test how help changes? like adding "test_help_type_and_no_action")

@PabloLION
Copy link
Contributor Author

Also I find another problem: we pass in choices=None it works differently here

if "choices" in kwargs and not isinstance(kwargs["choices"], (list, tuple)):

Should I create another PR on this?

Copy link
Member

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue #685 also mentioned the case type=None so I was expecting a test for this.

@mauvilsa
Copy link
Member

mauvilsa commented Mar 3, 2025

Also I find another problem: we pass in choices=None it works differently here

if "choices" in kwargs and not isinstance(kwargs["choices"], (list, tuple)):

Should I create another PR on this?

How does it work differently? I think it could be part of this pull request.

@mauvilsa
Copy link
Member

mauvilsa commented Mar 3, 2025

When creating remote branch, the git hook fired and I got this

mypy................................................................................Failed

I see. I think I need to specify in the pre-commit config which python version to run mypy with. No problem that you skipped it.

Also, I didn't have mypy/ruff installed (which made. Maybe we can consider adding them to the contributing guideline? I saw them in pyproject.toml but I could format my code :-( without them.

mypy/ruff don't need to be installed. It is pre-commit the one that installs them in a separate environment. If you want to only run ruff do

pre-commit run -a ruff

to only run mypy, do

pre-commit run -a --hook-stage pre-push mypy

PabloLION and others added 3 commits March 3, 2025 23:46
Co-authored-by: Mauricio Villegas <[email protected]>
Co-authored-by: Mauricio Villegas <[email protected]>
@PabloLION
Copy link
Contributor Author

Also I find another problem: we pass in choices=None it works differently here

if "choices" in kwargs and not isinstance(kwargs["choices"], (list, tuple)):

Should I create another PR on this?

How does it work differently? I think it could be part of this pull request.

When action is not None, passing choices=None, there will be similar problem with type=None.
But I have more doubts about choices: I'm creating a parser with dataclass where some fields are Enum type, and only a part of the Enum should be used. (Enum CLI args are matched by the name instead of value.)
In this case, jsonargparse will check if the parsed value is in choices, not the user input. However, the help test is only showing the parsed option, not what user should input.

I meant to create a new issue but I have not read the document nor have I tested it yet. Do you think this worths a shot?

@PabloLION
Copy link
Contributor Author

mypy/ruff don't need to be installed

Without them, I cannot format the code effectively. The suggestion was to add a part to mentioning the toolkit we are using in this project for more consistent code style.

Copy link
Member

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an entry to the changelog. After that, this would be good to merge.

With type=None and action=None, there's no problem with the old code. Should we still add the test?

Okay, fine. Then lets leave it like that.

When action is not None, passing choices=None, there will be similar problem with type=None.
But I have more doubts about choices: I'm creating a parser with dataclass where some fields are Enum type, and only a part of the Enum should be used. (Enum CLI args are matched by the name instead of value.)
In this case, jsonargparse will check if the parsed value is in choices, not the user input. However, the help test is only showing the parsed option, not what user should input.

I meant to create a new issue but I have not read the document nor have I tested it yet. Do you think this worths a shot?

Okay, it does sound like this needs a specific issue to address.

@mauvilsa
Copy link
Member

mauvilsa commented Mar 4, 2025

mypy/ruff don't need to be installed

Without them, I cannot format the code effectively. The suggestion was to add a part to mentioning the toolkit we are using in this project for more consistent code style.

Can you explain what you mean by "I cannot format the code effectively"? The code will be automatically formatted when pre-commit runs. And even if pre-commit is skipped, a github action will format it.

@PabloLION
Copy link
Contributor Author

I added the change log and I think it's ready to merge.

Can you explain what you mean by "I cannot format the code effectively"? The code will be automatically formatted when pre-commit runs. And even if pre-commit is skipped, a github action will format it.

I wanted to say that I avoided formatting the code intentionally because my line length was set to 80 instead of 120 on my local environment because I didn't know about the GitHub action. And Pylance was reporting this everywhere

"_ActionPrintConfig" is private and used outside of the module in which it is declared (Pylance) 
[reportPrivateUsage](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportPrivateUsage)

And about the Union type deprecation "This type is deprecated as of Python 3.10" etc. I guess our env was very different and I didn't change my python interpreter version (I'm exactly suffering for "mac313").

Anyway, thanks for your patience and guidance in this PR. I'll create another issue on the "choices" args.

Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (c3d3df7) to head (0fbc4c4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #687   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         6688      6688           
=========================================
  Hits          6688      6688           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing!

@mauvilsa mauvilsa merged commit ec382f1 into omni-us:main Mar 5, 2025
30 checks passed
@mauvilsa
Copy link
Member

mauvilsa commented Mar 5, 2025

Can you explain what you mean by "I cannot format the code effectively"? The code will be automatically formatted when pre-commit runs. And even if pre-commit is skipped, a github action will format it.

I wanted to say that I avoided formatting the code intentionally because my line length was set to 80 instead of 120 on my local environment because I didn't know about the GitHub action. And Pylance was reporting this everywhere

"_ActionPrintConfig" is private and used outside of the module in which it is declared (Pylance) 
[reportPrivateUsage](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportPrivateUsage)

And about the Union type deprecation "This type is deprecated as of Python 3.10" etc. I guess our env was very different and I didn't change my python interpreter version (I'm exactly suffering for "mac313").

Thanks for the info! I will do some changes and probably ping you for review.

@PabloLION PabloLION deleted the fix/type-action-restriction branch March 5, 2025 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Providing both type and action not allowed" check too strict
2 participants