Skip to content

Conversation

Pijukatel
Copy link
Collaborator

Description

  • Add a simple util function to limit the number of kwargs and reuse it in the code.

Testing

  • Added unit tests.

@github-actions github-actions bot added this to the 124th sprint - Tooling team milestone Sep 23, 2025
@github-actions github-actions bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels Sep 23, 2025
@Pijukatel Pijukatel added the adhoc Ad-hoc unplanned task added during the sprint. label Sep 23, 2025
@Pijukatel Pijukatel requested a review from vdusek September 23, 2025 07:37
@Pijukatel Pijukatel marked this pull request as ready for review September 23, 2025 07:38
@janbuchar janbuchar self-requested a review September 23, 2025 09:24
specified_params = sum(1 for param in [id, name, alias] if param is not None)
if specified_params > 1:
raise ValueError('Only one of "id", "name", or "alias" can be specified, not multiple.')
limit_kwarg_count(alias=alias, name=name, id=id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about the name, limit_kwarg_count doesn't really suggest that this line will throw if there's more than one non-None value passed to the function. Also having to repeat each kwarg twice is pretty unwieldy.

Copy link
Collaborator Author

@Pijukatel Pijukatel Sep 23, 2025

Choose a reason for hiding this comment

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

Using more explicit name.

Also having to repeat each kwarg twice is pretty unwieldy.

That is to have the names of the arguments available in the raised error. Is there a more clever way?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a more clever way?

I don't think so, unless you want to do some real voodoo (like stack frame inspection).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In that case, let's keep it this way for now. This is clear and readable.

"""Raise ValueError if there are more kwargs then max_kwargs."""
if len(kwargs) - Counter(kwargs.values())[None] > max_kwargs:
kwargs_names = [f'"{kwarg_name}"' for kwarg_name in kwargs]
raise ValueError(f'Only one of {", ".join(kwargs_names)} can be specified, not multiple.')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also tell the user which ones were specified?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, added to the exception text

@Pijukatel Pijukatel requested a review from janbuchar September 24, 2025 08:22
Comment on lines 7 to 9
if len(kwargs) - Counter(kwargs.values())[None] > max_kwargs:
kwargs_names = [f'"{kwarg_name}"' for kwarg_name in kwargs]
used_kwargs_names = [f'"{kwarg_name}"' for kwarg_name, value in kwargs.items() if value is not None]
Copy link
Collaborator

@Mantisus Mantisus Sep 25, 2025

Choose a reason for hiding this comment

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

Since we need to use list comprehension for used_kwargs_names anyway, do we really need Counter?

maybe just

used_kwargs_names = [f'"{kwarg_name}"' for kwarg_name, value in kwargs.items() if value is not None]
if len(used_kwargs_names ) > max_kwargs:

@Pijukatel Pijukatel merged commit 58e6a86 into master Sep 26, 2025
19 checks passed
@Pijukatel Pijukatel deleted the limit-kwarg-count branch September 26, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants