beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [beam] chadrik commented on a change in pull request #10822: [BEAM-7746] Minor typing updates / fixes
Date Wed, 26 Feb 2020 22:31:22 GMT
chadrik commented on a change in 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 (i.e. as with the original code), 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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message