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-7746] Add python type hints (part 1) #9915

Merged
merged 17 commits into from
Dec 11, 2019

Conversation

chadrik
Copy link
Contributor

@chadrik chadrik commented Oct 29, 2019

This is part 1 of #9056


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Choose reviewer(s) and mention them in a comment (R: @username).
  • 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.

See the Contributor Guide for more tips on how to make review process smoother.

Post-Commit Tests Status (on master branch)

Lang SDK Apex Dataflow Flink Gearpump Samza Spark
Go Build Status --- --- Build Status --- --- Build Status
Java Build Status Build Status 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
--- Build Status
Build Status
Build Status
Build Status
--- --- Build Status
XLang --- --- --- Build Status --- --- ---

Pre-Commit Tests Status (on master branch)

--- Java Python Go Website
Non-portable Build Status Build Status
Build Status
Build Status Build Status
Portable --- Build Status --- ---

See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.

@chadrik
Copy link
Contributor Author

chadrik commented Oct 29, 2019

R: @robertwb

@chadrik
Copy link
Contributor Author

chadrik commented Oct 29, 2019

Run Python PreCommit

Copy link
Contributor

@robertwb robertwb left a comment

Choose a reason for hiding this comment

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

I've looked at everything up to apache_beam/runners/common.py.

sdks/python/apache_beam/coders/coder_impl.py Show resolved Hide resolved
sdks/python/apache_beam/coders/coder_impl.py Show resolved Hide resolved
sdks/python/apache_beam/coders/coder_impl.py Show resolved Hide resolved
sdks/python/apache_beam/coders/coder_impl.py Show resolved Hide resolved
sdks/python/apache_beam/coders/coders.py Show resolved Hide resolved
sdks/python/apache_beam/io/iobase.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/io/iobase.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/io/iobase.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/io/textio.py Show resolved Hide resolved
@chadrik
Copy link
Contributor Author

chadrik commented Nov 12, 2019

Just a friendly reminder to nudge this along.

Copy link
Contributor

@robertwb robertwb left a comment

Choose a reason for hiding this comment

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

Reviewed up through runners/portability.

@chadrik
Copy link
Contributor Author

chadrik commented Nov 18, 2019

Reviewed up through runners/portability.

You're almost 2/3 of the way through. Almost there :)

Copy link
Contributor

@robertwb robertwb left a comment

Choose a reason for hiding this comment

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

Reviewe everything up through transforms/ptransform.py

sdks/python/apache_beam/runners/runner.py Show resolved Hide resolved
sdks/python/apache_beam/runners/worker/bundle_processor.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/runners/worker/bundle_processor.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/runners/worker/bundle_processor.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/runners/worker/bundle_processor.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/transforms/core.py Show resolved Hide resolved
sdks/python/apache_beam/transforms/core.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/transforms/core.py Show resolved Hide resolved
sdks/python/apache_beam/transforms/core.py Show resolved Hide resolved
@chadrik
Copy link
Contributor Author

chadrik commented Nov 22, 2019

We're so close. Just a few more. Thanks for your hard work getting through all of this!

Copy link
Contributor

@robertwb robertwb left a comment

Choose a reason for hiding this comment

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

Thanks. Just a couple of comments, but it all LGTM. Unsurprisingly there's some merge conflicts, but hopefully they should be easy to resolve.

sdks/python/apache_beam/transforms/window.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/transforms/window.py Outdated Show resolved Hide resolved
@@ -505,7 +527,8 @@ def get_window_coder(self):
return coders.IntervalWindowCoder()

def merge(self, merge_context):
to_merge = []
# type: (WindowFn.MergeContext) -> None
to_merge = [] # type: List[Union[IntervalWindow, GlobalWindow]]
Copy link
Contributor

Choose a reason for hiding this comment

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

List[BoundedWindow]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this and the other use of Union[IntervalWindow, GlobalWindow] to BoundedWindow results in these errors:

apache_beam/transforms/window.py:541: error: "BoundedWindow" has no attribute "start"  [attr-defined]
apache_beam/transforms/window.py:543: error: "BoundedWindow" has no attribute "start"  [attr-defined]
apache_beam/transforms/window.py:550: error: "BoundedWindow" has no attribute "start"  [attr-defined]
apache_beam/transforms/window.py:557: error: "BoundedWindow" has no attribute "start"  [attr-defined]

The docs for BoundedWindow say that it's for timestamps in the range (-infinity, end).

How about we add a start property to BoundedWindow:

class BoundedWindow(object):
  """A window for timestamps in range (-infinity, end).

  Attributes:
    end: End of window.
  """

  def __init__(self, end):
    # type: (Union[int, float, Timestamp]) -> None
    self._end = Timestamp.of(end)

  @property
  def start(self):
    # type: () -> Timestamp
    return MAX_TIMESTAMP

sdks/python/apache_beam/utils/timestamp.py Outdated Show resolved Hide resolved
@@ -136,14 +140,17 @@ def to_rfc3339(self):
return self.to_utc_datetime().isoformat() + 'Z'

def __float__(self):
# type: () -> float
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised mypy doesn't know these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It may, but I didn't test it.

sdks/python/apache_beam/utils/windowed_value.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/utils/windowed_value.py Outdated Show resolved Hide resolved
sdks/python/mypy.ini Outdated Show resolved Hide resolved
sdks/python/setup.py Outdated Show resolved Hide resolved
@chadrik chadrik force-pushed the python-static-typing-part1 branch 6 times, most recently from 5657f26 to d7963d4 Compare November 24, 2019 17:08
@chadrik
Copy link
Contributor Author

chadrik commented Nov 25, 2019

Run Python PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Nov 25, 2019

@udim pytest is causing failures in the setup of the portable runner tests. It has something to do with the use of setup_requires to install pytest-runner in setup.py. This feature of setuptools has been abandoned for a long time, and the pytest-runner PyPI page says that it's deprecated and should not be used.

I've created a PR to try to modernize and simplify the python build process, especially as it relates to build requirements: #10038. I'm still ironing out some details. Let me know what you think.

In the meantime I think we should remove the use of pytest_runner and setup_requires.

@chadrik
Copy link
Contributor Author

chadrik commented Nov 25, 2019

Run Python PreCommit

2 similar comments
@chadrik
Copy link
Contributor Author

chadrik commented Nov 25, 2019

Run Python PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Nov 26, 2019

Run Python PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Dec 2, 2019

Run Python PreCommit

@chadrik chadrik force-pushed the python-static-typing-part1 branch 2 times, most recently from c1a02d1 to 7a40e82 Compare December 2, 2019 22:25
@chadrik
Copy link
Contributor Author

chadrik commented Dec 2, 2019

Run Java PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Dec 3, 2019

Run Python PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Dec 3, 2019

all tests are passing and there are no conflicts with master!

@chadrik
Copy link
Contributor Author

chadrik commented Dec 3, 2019

@robertwb I addressed all of your notes in a final commit so that you can compare them against your notes and ensure they are resolved to your liking.

@chadrik
Copy link
Contributor Author

chadrik commented Dec 4, 2019

merge conflicts again....

Many of these (those marked with [misc]) are due to mypy taking issue with:
- the use of invalid type objects (typehints not typing)
- the use of type objects at runtime
`None` is only safe to pass as the context to Coder.to_runner_api() when we know that the Coder class does not have components, otherwise the context is used to find and convert them.  We could remove the need for special case `ignores` by reorganizing our Coder class hierarchy to add a CompoundCoder base class or mixin.

Right now this is the only place in the code that we need to ignore this problem, so it's not worth tackling now.
This problem will be fixed when the next version of mypy is released
The test runs but error status is ignored.
This resolves a pip conflict that was happening between setupVirtualenv task
and the tox task by ensuring that everything agrees on the same set of versions.
There are no meaningful runtime changes in this commit.
This seems to be fine to do on an instance as long as the types match, but not on a class.
@chadrik
Copy link
Contributor Author

chadrik commented Dec 10, 2019

Run Java PreCommit

@chadrik
Copy link
Contributor Author

chadrik commented Dec 10, 2019

Run Python PreCommit

1 similar comment
@chadrik
Copy link
Contributor Author

chadrik commented Dec 10, 2019

Run Python PreCommit

@pabloem pabloem merged commit df37616 into apache:master Dec 11, 2019
@udim
Copy link
Member

udim commented Dec 13, 2019

I believe this PR broke :sdks:python:test-suites:direct:py37:hdfsIntegrationTest
Opened https://issues.apache.org/jira/browse/BEAM-8966

@chadrik
Copy link
Contributor Author

chadrik commented Dec 13, 2019 via email

@udim
Copy link
Member

udim commented Dec 16, 2019

Is that not part of the precommit suite? The tests for this PR definitely ran and passed. The traceback seemed to indicate that google.protobuf is not installed...

On Fri, Dec 13, 2019 at 11:31 AM Udi Meiri @.***> wrote: I believe this PR broke :sdks:python:test-suites:direct:py37:hdfsIntegrationTest Opened https://issues.apache.org/jira/browse/BEAM-8966 — You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub <#9915?email_source=notifications&email_token=AAAPOE3TOM63EE2P72YWOTTQYPPHNA5CNFSM4JGC3N42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEG3AO5Y#issuecomment-565577591>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAPOE3HEVMZEZOXV2MHHM3QYPPHNANCNFSM4JGC3N4Q .

Not it is not part of precommits.

dpcollins-google pushed a commit to dpcollins-google/beam that referenced this pull request Dec 20, 2019
* Address issues with a few missing modules/objects

* Ignore some objects missing from typeshed

* Ignore an issue regarding asymmetrical property getter/setter

* Ignore unavoidable errors

Many of these (those marked with [misc]) are due to mypy taking issue with:
- the use of invalid type objects (typehints not typing)
- the use of type objects at runtime

* Ignore some conditional imports in tests

* Ignore functions missing from typeshed

* Add tests for Timestamp/Duration comparisons

* Ignore errors about dynamic base classes

This problem will be fixed when the next version of mypy is released

* Ignore some monkey-patching for tests

* Consolidate python build requirements

This resolves a pip conflict that was happening between setupVirtualenv task
and the tox task by ensuring that everything agrees on the same set of versions.

* Ignore a difficult scenario where None is sometimes unsafe

`None` is only safe to pass as the context to Coder.to_runner_api() when we know that the Coder class does not have components, otherwise the context is used to find and convert them.  We could remove the need for special case `ignores` by reorganizing our Coder class hierarchy to add a CompoundCoder base class or mixin.

Right now this is the only place in the code that we need to ignore this problem, so it's not worth tackling now.

* Add a mypy lint job

The test runs but error status is ignored.

* Workaround a bug in pylint

pylint-dev/pylint#3217

* Add type annotations to bulk of python codebase

There are no meaningful runtime changes in this commit.

* Silence errors about overriding methods.

This seems to be fine to do on an instance as long as the types match, but not on a class.

* Address review notes

* Rebase fixup
@@ -178,6 +179,7 @@ indent-after-paren=4
ignore-long-lines=(?x)
(^\s*(import|from)\s
|^\s*(\#\ )?<?(https?|ftp):\/\/[^\s\/$.?#].[^\s]*>?$
|# type:
Copy link
Contributor

Choose a reason for hiding this comment

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

This line disabled line-too-long errors across the board: https://issues.apache.org/jira/browse/BEAM-9058.

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.

5 participants