[ 
https://issues.apache.org/jira/browse/BEAM-7746?focusedWorklogId=393807&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-393807
 ]

ASF GitHub Bot logged work on BEAM-7746:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 26/Feb/20 22:30
            Start Date: 26/Feb/20 22:30
    Worklog Time Spent: 10m 
      Work Description: chadrik commented on pull request #10822: [BEAM-7746] 
Minor typing updates / fixes
URL: https://github.com/apache/beam/pull/10822#discussion_r384807421
 
 

 ##########
 File path: sdks/python/apache_beam/transforms/external_java.py
 ##########
 @@ -37,18 +39,19 @@
 
 # Protect against environments where apitools library is not available.
 # pylint: disable=wrong-import-order, wrong-import-position
+apiclient = None  # type: Optional[types.ModuleType]
 
 Review comment:
   > My question is why we need to type apiclient as types.ModuleType at all.
   
   Fair question.  
   
   If we don't do this, we get the following error:
   ```
   apache_beam/transforms/external_java.py:46: error: Incompatible types in 
assignment (expression has type "None", variable has type Module)  [assignment]
   ```
   
   To resolve this, we need to mark `apiclient` as `Optional` 
   
   --- 
   why can't we save ourselves some headache and leave out of the 
`types.ModuleType` part?
   
   ```python
   apiclient = None  # type: Optional
   try:
     from apache_beam.runners.dataflow.internal import apiclient
   except ImportError:
     pass
   ```
    
   If we do this, `apiclient` will become `Optional[Any]`
   
   ---
   
   Why can't we ignore the error?  
   
   We can, but then mypy will mark the type as non-Optional, and that would 
remove the added protections that mypy provides against accidentally using the 
variable when it's `None`.
   
   
   ---
   
   Why can't we just add the type comment on the original `apiclient = None` 
line? 
   
   ```python
   try:
     from apache_beam.runners.dataflow.internal import apiclient
   except ImportError:
     apiclient = None  # type: Optional[types.ModuleType]
   ```
   
   With this, we get the following error:
   
   ```
   apache_beam/transforms/external_java.py:46: error: Name 'apiclient' already 
defined (by an import)  [no-redef]
   ```
   
   There is only one opportunity to override/influence the inferred type of a 
variable:  on the first line where it is defined (think of type variable 
definitions like C/C++, but with python scoping rules).  However, `apiclient` 
is defined via an import rather than an assignment, which forces us to preface 
the import with a variable definition.
   
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 393807)
    Time Spent: 66h 40m  (was: 66.5h)

> Add type hints to python code
> -----------------------------
>
>                 Key: BEAM-7746
>                 URL: https://issues.apache.org/jira/browse/BEAM-7746
>             Project: Beam
>          Issue Type: New Feature
>          Components: sdk-py-core
>            Reporter: Chad Dombrova
>            Assignee: Chad Dombrova
>            Priority: Major
>          Time Spent: 66h 40m
>  Remaining Estimate: 0h
>
> As a developer of the beam source code, I would like the code to use pep484 
> type hints so that I can clearly see what types are required, get completion 
> in my IDE, and enforce code correctness via a static analyzer like mypy.
> This may be considered a precursor to BEAM-7060
> Work has been started here:  [https://github.com/apache/beam/pull/9056]
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to