Oh thanks, I had no idea that existed. I wanted to experiment with Mapping and unions but it's not working. I couldn't find any existing issues about this. Should I be opening an issue? If not then there's no need to explain what I'm doing wrong.
``` $ cat test.py print({} | {}) $ ./python test.py {} $ mypy --python-version 3.9 test.py test.py:1: error: Unsupported left operand type for | ("Dict[<nothing>, <nothing>]") Found 1 error in 1 file (checked 1 source file) $ mypy --version mypy 0.782 ``` On Wed, Aug 26, 2020 at 11:18 PM Guido van Rossum <gu...@python.org> wrote: > Use typing.AbstractSet for the ABC (there was an unfortunate naming > conflict -- we have dict and Mapping, list and Sequence, but set and Set...) > > On Wed, Aug 26, 2020 at 2:16 PM Alex Hall <alex.moj...@gmail.com> wrote: > >> 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. >> > > > -- > --Guido van Rossum (python.org/~guido) > *Pronouns: he/him **(why is my pronoun here?)* > <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/> >
_______________________________________________ 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/CBLLORSVOSCWK2STUAIOVJAPR3L4LL4R/ Code of Conduct: http://python.org/psf/codeofconduct/