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

[BEAM-5878] [BEAM-5490] Add partial support for functions with keyword-only arguments. #6781

Merged
merged 2 commits into from
Oct 31, 2018

Conversation

tvalentyn
Copy link
Contributor

@tvalentyn tvalentyn commented Oct 22, 2018

Fix a crash in Beam codebase reported by TFX team that happens when a pipeline operates with functions that take keyword-only arguments (a language feature that became available in Python 3). This PR is a first step to support kwonlyargs, there are a few other places in Beam codebase captured in TODOs where additional work may be needed. We will also need to add (Python 3-only) tests where dofns/sideinputs have kwonlyargs once Python 3 test suite is in a reasonably-good shape.

  1. Use getfullargspec instead of getargspec on Python 3, which recognizes kwonlyargs
  2. Avoid an irreverisble monkey-patch of inspect.getargspec. ([BEAM-5490])
  3. Recognize functions with kwonlyargs in sideinputs.

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Format the pull request title like [BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replace BEAM-XXX with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

It will help us expedite review of your Pull Request if you tag someone (e.g. @username) to look at it.

Post-Commit Tests Status (on master branch)

Lang SDK Apex Dataflow Flink Gearpump Samza Spark
Go Build Status --- --- --- --- --- ---
Java Build Status Build Status Build Status Build Status Build Status Build Status Build Status Build Status
Python Build Status --- Build Status
Build Status
Build Status --- --- ---

@tvalentyn tvalentyn force-pushed the fix_getargspec branch 5 times, most recently from e12def5 to d116395 Compare October 29, 2018 19:16
@tvalentyn tvalentyn changed the title [BEAM-5322] Make decorators.getargspec Python 3-compatible. [BEAM-5878] [BEAM-5490] Add partial support for functions with keyword-only arguments. Oct 29, 2018
@tvalentyn
Copy link
Contributor Author

Run Dataflow Python ValidatesRunner

@tvalentyn
Copy link
Contributor Author

Run Python Dataflow ValidatesRunner

@tvalentyn
Copy link
Contributor Author

tvalentyn commented Oct 29, 2018

R: @aaltay
CC: @robertwb @splovyt @RobbeSneyders @markflyhigh
Also thanks to @ruoyu who did initial investigation and came up with a patch.

varkw = argspec.keywords
kwonlyargs = []

return (len(argspec.args) + len(kwonlyargs) > 1 + is_bound or
Copy link
Member

Choose a reason for hiding this comment

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

why len(kwonlyargs) > 1 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We preserve the original intent: to check that total number of arguments is more than 1, or 2 if the method is bound.

# Arguments with the %unknown% prefix will be ignored in the type
# checking code.
return inspect.ArgSpec(
['_'], '__unknown__varargs', '__unknown__keywords', ())
try:
Copy link
Member

Choose a reason for hiding this comment

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

You can use _use_legacy_getargspec. You do not need another try/except.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

try:
callargs = inspect.getcallargs(func, *packed_typeargs, **typekwargs)
except TypeError as e:
raise TypeCheckError(e)
finally:
# Revert monkey-patch.
Copy link
Member

Choose a reason for hiding this comment

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

I think we should fully stop doing the monkey patching.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Added a TODO to the relevant JIRA issue. We can do that in a separate change.

…d-only arguments: use getfullargspec instead of getargspec on Python 3, which recognizes kwonlyargs, avoid an irreverisble monkey-patch of inspect.getargspec.
@tvalentyn
Copy link
Contributor Author

@aaltay Thanks for the review! PTAL.

@aaltay aaltay merged commit aab2f75 into apache:master Oct 31, 2018
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.

2 participants