On Wed, Aug 26, 2020 at 10:10 PM Christopher Barker <python...@gmail.com>
wrote:

> Honestly, I haven't delved into type checking much, so maybe I'm making
> this up, but what if you write a function that is type hinted to take a
> Mapping, and uses | or |=, and all the test code uses a built in dict, and
> all seems to be well.
>

This made me curious so I did an experiment, but with Set because I
couldn't be bothered to worry about Python versions. This code:

```

import typing
import collections
import builtins

def foo(t: typing.Set, c: collections.Set, b: builtins.set):
    print(t + t)
    print(t | t)
    print(t.union(t))

    print(c + c)
    print(c | c)
    print(c.union(c))

    print(b + b)
    print(b | b)
    print(b.union(b))

```

complains about `+` (as it should) and `c.union` in both PyCharm and mypy,
and nothing else. So typing.Set is based on the builtin and not the ABC. If
you want to support any subclass of collections.Set, your type hint needs
to be collections.Set. Using typing.Set will fail to show warnings when you
use a builtin only method, and will show warnings when you try to assign an
ABC set (e.g. I get a warning with `foo(t=<instance of
collections.abc.Set>)`. The problem then is that `collections.abc.Set[int]`
doesn't work, i.e. you need to use typing.Set if you want generics.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4LJ6RMIJXE4WFRTCAK4B4HUQL4ATOETY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to