[Python-ideas] Optional static typing -- the crossroads

Steven D'Aprano steve at pearwood.info
Sun Aug 17 11:51:52 CEST 2014


On Sun, Aug 17, 2014 at 02:44:32AM -0700, Ɓukasz Langa wrote:
> On Aug 17, 2014, at 2:33 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> 
> > Stefan Behnel wrote:
> >> However, it does not support protocols, so it still needs
> >> something that allows us to say Iterable(int) in some way.
> > 
> > Just had a thought -- does mypy provide a way to express
> > a type that supports more than one protocol? E.g. can you
> > say that something must be both Iterable and Hashable?
> 
> That would be union types. Current syntax:
> Union[Iterable[int], Hashable]

I don't think so. The mypy tutorial says:

    Use the Union[...] type constructor to construct a union type. For 
    example, the type Union[int, str] is compatible with both integers 
    and strings. You can use an isinstance check to narrow down the type 
    to a specific type ...

which implies that x:Union[a, b] means:

    isinstance(x, (a, b))

rather than:

    isinstance(x, a) and isinstance(x, b)

which I think is what Greg is asking for.




-- 
Steven


More information about the Python-ideas mailing list