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 CapturedCallable to work better with kwargs #121

Merged
merged 6 commits into from
Oct 25, 2023

Conversation

antonymilne
Copy link
Contributor

@antonymilne antonymilne commented Oct 23, 2023

Description

#114 uncovered a bug with CapturedCallable. In short, this would work ok:

function = lambda **kwargs: kwargs["a"]

CapturedCallable(function)(a=2) # a=2 only provided at runtime

And this would not:

CapturedCallable(function, a=2)() # a=2 is provided upfront

Now they should both work ok.

We have already had a couple of very small bugs that I've known about for a while, where the following sorts of functions do not work with CapturedCallable:

  1. a function with variadic positional args like def function(*args)
  2. a function with positional-only arguments like def function(a, /)

Fixing the new **kwargs bug, which is an important case, makes it much harder to fix these much less important cases, and so I've decided to just not support them at all. There's now an explicit check to make sure no one tries to use CapturedCallable with those sorts of functions (very unlikely they'd want to do so anyway).

Tests have been updated to be much more thorough so that we now test every case on all 3 sorts of valid function positional_or_keyword_function, keyword_only_function, var_keyword_function.

Thanks to @maxschulz-COL for spotting the bug and for his initial fix which is the basis for this!

Checklist

  • I have not referenced individuals, products or companies in any commits, directly or indirectly
  • I have not added data or restricted code in any commits, directly or indirectly
  • I have updated the docstring of any public function/class/model changed
  • I have added the PR number to the change description in the changelog fragment, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1)) (if applicable)
  • I have added tests to cover my changes (if applicable)

Types of changes

  • Docs/refactoring (non-breaking change which improves codebase)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Notice

  • I acknowledge and agree that, by checking this box and clicking "Submit Pull Request":

    • I submit this contribution under the Apache 2.0 license and represent that I am entitled to do so on behalf of myself, my employer, or relevant third parties, as applicable.
    • I certify that (a) this contribution is my original creation and / or (b) to the extent it is not my original creation, I am authorized to submit this contribution on behalf of the original creator(s) or their licensees.
    • I certify that the use of this contribution as authorized by the Apache 2.0 license does not violate the intellectual property rights of anyone else.

Copy link
Contributor

@petar-qb petar-qb left a comment

Choose a reason for hiding this comment

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

I like the compromise this PR brings (fixing a bug with variadic keyword arguments while not supporting positional only and variadic positional arguments) and think this will not adversely affect custom actions and custom graphs since we always send function arguments through the Parameter, and custom action mechanism in a keyword format.

Also thanks for the comments and tests in the code 💯 (they help lot to understand the problem and implemented solution).

So, +1 approval from my side. 🥇

Just update the changelog.d directory (with hatch run docs:changelod) with described bugfix (cuz it affects our users) and tick corresponding checkboxes under the Checklist and Types of changes in the PR Description 😄

vizro-core/src/vizro/models/types.py Show resolved Hide resolved
…captured-callable

# Conflicts:
#	vizro-core/changelog.d/20231024_101744_antony.milne_captured_callable.md
@petar-qb petar-qb mentioned this pull request Oct 24, 2023
16 tasks
Copy link
Contributor

@maxschulz-COL maxschulz-COL left a comment

Choose a reason for hiding this comment

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

Really great work! I just have one concern which I may have agreed to last night not grasping the full consequence, but maybe I am wrong. Let's see!

Thanks also for the extensive tests, really a joy to read actually!

vizro-core/src/vizro/models/types.py Show resolved Hide resolved
vizro-core/src/vizro/models/types.py Show resolved Hide resolved
Copy link
Contributor

@huong-li-nguyen huong-li-nguyen left a comment

Choose a reason for hiding this comment

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

Thanks for this, looks great! 🚀 Was mostly reading through this out of interest, the tests really helped understanding it. Is it worth adding a note to the docs for the custom charts/actions or do you think the error messages suffice to provide guidance? Either way is fine for me 👍

…allable.md

Co-authored-by: Li Nguyen <90609403+huong-li-nguyen@users.noreply.github.com>
Signed-off-by: Antony Milne <49395058+antonymilne@users.noreply.github.com>
@antonymilne
Copy link
Contributor Author

Thanks for this, looks great! 🚀 Was mostly reading through this out of interest, the tests really helped understanding it. Is it worth adding a note to the docs for the custom charts/actions or do you think the error messages suffice to provide guidance? Either way is fine for me 👍

I think no need for anything in the narrative docs, since a user is so unlikely to ever try to do CapturedCallable on a function where it doesn't work that having any note on this is more likely to just confuse things. I will make a note of it in the docstring though!

@antonymilne antonymilne enabled auto-merge (squash) October 25, 2023 08:46
@antonymilne antonymilne merged commit 1cfa247 into main Oct 25, 2023
25 checks passed
@antonymilne antonymilne deleted the bug/captured-callable branch October 25, 2023 08:52
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.

None yet

4 participants