I'm not sure if this is the right place to bring this up, python-ideas seemed 
like language issues and python-dev seemed like CPython issues.

There are several unhashable builtin types present in CPython, as of today the 
ones I've noticed are: lists, dicts, sets, and bytearrays.
Two of these are containers that require (at least partially) their members to 
be hashable: dicts and sets

By this logic you cannot have a set of lists or a dict of sets to ints.

CPython however does not stop or warn you if you attempt to do such a thing 
until it hits `BUILD_MAP` or `BUILD_SET` during runtime.
This is reasonable behavior when it's not possible to infer the member type of 
the container i.e. ``{f(x) for x in iterable}`` or ``{f(x): y for zip(xs, ys)}``

However, given the situation where literals are nested i.e. ``{[*gen] for gen 
in gens}`` or ``{{green: eggs}, {and_: ham}}`` this presents an unavoidable 
exception at runtime.
I suggest emitting a SyntaxWarning when encountering these cases of literals 
that produce unhashable types that are used in literals that produce types 
where the members must be hashable.
I don't think it should be a SyntaxError because it's not a language issue, its 
an implementation issue.
I don't think it should be a linters responsibility because for the most part 
linters should consider language issues/idioms not side-effects from the 
running implementation.

I do understand that such cases this issue addresses may be uncommon and once 
you do get that TypeError raised its a relatively quick and easy fix, but 
consider this being present in code paths that don't get taken as frequently, 
large codebases where it becomes difficult to keep track of small one liner 
literals like this or even for the newer programmers toying with Python through 
CPython and naively using unhashables in places they shouldn't be.

Either way I'm interested in hearing what the core team thinks of this 
suggestion, thanks in advance! :D
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/IUOIEOCI22N3G67HX5BOKBS7Z26KC4FX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to