chadrik commented on a change in pull request #10822: [BEAM-7746] Minor typing
updates / fixes
URL: https://github.com/apache/beam/pull/10822#discussion_r384689098
##########
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:
I completely agree. Unfortunately, this is a syntax error in mypy:
```python
try:
from apache_beam.runners.dataflow.internal import apiclient # type:
Optional[types.ModuleType]
except ImportError:
apiclient = None
```
As is this:
```python
try:
import apache_beam.runners.dataflow.internal.apiclient as apiclient #
type: Optional[types.ModuleType]
except ImportError:
apiclient = None
```
The reason is that type comments are (by design) not capable of doing
anything that the new PEP 526 variable annotations are not. In other words,
this is obviously wrong:
```python
try:
Optional[types.ModuleType]: import
apache_beam.runners.dataflow.internal.apiclient as apiclient
except ImportError:
apiclient = None
```
In that context, this makes more sense:
```python
apiclient: Optional[types.ModuleType] = None
try:
from apache_beam.runners.dataflow.internal import apiclient
except ImportError:
pass
```
----------------------------------------------------------------
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]
With regards,
Apache Git Services