osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't define a pytype alias from Beam's PCollection type.


On Wed, Nov 7, 2018 at 10:30 PM Zach Moshe <zmoshe@xxxxxxxxxx> wrote:
>
> (Adding the public Beam-dev group)
>
> On Wed, Nov 7, 2018 at 2:26 PM Zach Moshe <zmoshe@xxxxxxxxxx> wrote:
>>
>> Hi,
>> I've noticed that `beam.core.pvalue.PCollection` doesn't support a `__getitem__()` that returns a `GenericMeta` type (like regular types from typing do).
>>
>> While `PCollection[MyObject]` seems to work when used inside a method definition (e.g. `def func(coll: PCollection[MyObject]): ...`,

This is because that's not actually interpreted except by pytype. It
will also crash in Python 3.

>> it crashes when I try to create a type alias from it:
>>
>> MyCollection = PCollection[MyObject]
>>
>> TypeError: 'type' object has no attribute '__getitem__'
>>
>> Is that intended? Any reason why we shouldn't be able to create an alias?
>>
>> For some general context - my use-case is that I have an alias for the basic type I'm using throughout my module. I want to have another alias for a PCollection of these because I have several methods in the code which I want to annotate with pytypes and make sure they receive only a PCollection of MyObjects. It seems to be only supported if I copy the type definition to all methods instead of aliasing it as MyCollection and using the alias.
>>
>> To make it clear - I'm using regular pytype, not Beam's typehints (I want regular static type checking, not the pipeline's initialization one).
>>
>> I would be grateful if anyone, from either of the groups, could help me understand if I'm doing something wrong, expecting something that is intentionally not done, or it's some kind of a bug/missing-feature so I could mitigate appropriately.

It's just a missing feature. If you want to give
https://github.com/apache/beam/pull/6983 an LGTM it won't be anymore
:).

>> Many thanks,
>> Zach
>>
> --
> You received this message because you are subscribed to the Google Groups "dataflow-beam" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to dataflow-beam+unsubscribe@xxxxxxxxxx.
> To post to this group, send email to dataflow-beam@xxxxxxxxxx.
> To view this discussion on the web visit https://groups.google.com/a/google.com/d/msgid/dataflow-beam/CAD%3DREx2EinaqMwyNX1TcWTRpfTW4inqnoWrvTw5qkQuJW0o_-w%40mail.gmail.com.