raams95 opened a new pull request, #41614:
URL: https://github.com/apache/beam/pull/41614

   Fixes #20986.
   
   Using a `typing.Protocol` as a type hint currently fails with a raw 
`TypeError` from `typing`, for example `Instance and class checks can only be 
used with @runtime_checkable protocols`, with nothing pointing at the type hint 
or at what to change.
   
   This follows @jrmccluskey's suggestion on #40757 (closed for inactivity) 
instead of relaxing the check: type checking stays strict, but the failure now 
tells the author what to do. A new `ProtocolTypeHintError` (a `TypeError` 
subclass, so existing `except TypeError` handling still works) is raised from:
   
   - `check_constraint`, for a Protocol hint that is not decorated with 
`@typing.runtime_checkable`. This covers runtime and performance runtime type 
checking.
   - `is_consistent_with`, for a Protocol base that is not 
`@runtime_checkable`, or that is runtime checkable but has non-method members. 
`issubclass()` does not support those (as damccorm pointed out on #40757), so 
they get an error too rather than being silently accepted. Beam's `isinstance` 
check in `check_constraint` is unchanged for them.
   - `Union` type checking re-raises it instead of treating it as one more 
failed alternative.
   
   Runtime-checkable Protocols with only methods behave exactly as before.
   
   The error messages:
   
   ```
   Protocol type hint MyProtocol cannot be used for type checking because it is 
not decorated with @typing.runtime_checkable. Decorate it with 
@typing.runtime_checkable, or use a concrete class or typing.Any as the type 
hint instead.
   
   Protocol type hint MyProtocol has non-method members (value), so Beam cannot 
check other type hints against it. Use a @typing.runtime_checkable Protocol 
that only declares methods, or use a concrete class or typing.Any as the type 
hint instead.
   ```
   
   On the question of a concrete use case: no, there is no production use case 
on my side. I picked this up from the issue backlog, so a clear error seemed 
the better fix than relaxing the check.
   
   Tests (`typehints_test.py`, `typecheck_test.py`) cover both messages at 
`is_consistent_with` (with and without beartype), `check_constraint`, composite 
hints and `Union`, runtime and performance runtime type check pipelines, and 
that a `@runtime_checkable` method-only Protocol still works. The new error 
tests fail on master with the raw `TypeError` and pass with this change.
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us 
incorporate your contribution quickly and easily:
   
    - [x] Mention the appropriate issue in your description (for example: 
`addresses #123` or `fixes #456`).
    - [x] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual 
Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to