5 ms·
> which allow developers to say that a function returns Dict[List[Set[FrozenSet[int]]], str] this type is impossible in Python
by ghostwriter 5y ago
> which allow developers to say that a function returns Dict[List[Set[FrozenSet[int]]], str]
this type is impossible in Python
- mivade 5y agoCorrect. Keys must be immutable and hashable.
- pdonis 5y agoTechnically keys only have to be hashable, but an object that is hashable but not immutable is not a good idea and in practice the two will go together.
- gnulinux 5y agoNot only impossible in python, but also seems conceptually useless (why would you have a set of frozen sets?).
- yifanl 5y agoEnsure uniqueness, i guess? Though you'd lose that when you make the list of sets. I could maybe see something like this as an intermediary result of parsing some horrible, nested obscure message format (except the dict, that baffles me unless the key-value types are reversed?)
- raymondh 5y agoAlmost the entire purpose of frozensets was so that you could have sets of frozensets. It's the only way to have nested sets. Frozensets also work as dictionary keys.
- deltaonefour 5y agoThis is not useless. Why would you have a list of lists? A list is just an iterable container class, which is what a set is. If the need exists for a list of lists then it exists for a set of sets. An example of this is finding all combinations possible non ordered groups of letters given a collection of letters: abadfjiik. A typical algorithm will produce dupes. But if your accumulator is a set of frozen sets you can produce a result with no dupes.
- nisegami 5y agoI guarantee something like that is running in production somewhere in the world (maybe without the list part).
- lolive 5y agoWith this type system is it possible to subclass string and int, to enforce types for, let’s say, firstName, postalCode, etc.
- paisawalla 5y agoYes, simply FirstNameType = str (But don't use ints for postal codes, even if the postal codes are numeric)
- lolive 5y agoHuuum. If I sign a function so it only accepts a FirstNameType, and I use the statement you propose, won’t any string be acceptable as an input parameter, from the type system point of view?
- jamincan 5y agoYeah, that is the syntax for a type alias. You can use NewType to create an actual subclass of the type instead. https://docs.python.org/3/library/typing.html#newtype https://docs.python.org/3/library/typing.html#newtype
- paisawalla 5y agoIn that case, you need a subclass of string that does nothing but has its type as FirstNameType. Wherever you create or return strings that are first names, you'll need to instantiate FirstNameType instead.
- colejohnson66 5y ago> ... even if the postal codes are numeric Maybe in the US. In Canada, they are 6 characters of letter-digit-letter-digit-letter-digit (in regex: /((?:[A-Z][0-9]){3})/)
- kennu 5y agoAnd in the future, they could be anything. Finland expanded the allowed characters in person identification numbers to support people born after 2000. Usually bad idea to be too strict in checking fields where people manually enter text.
- lolive 5y agoDoes IntelliJ use any kind of static type analysis to help the user autocomplete properly? [== do we have something like Java???]
- deltaonefour 5y agoYes. Try pycharm.
- aenis 5y agoWithin reason. I use type annotations solely as means of documenting code, with the added benefit of having autosuggest work all of the time. I am one of those guys who put a lot of comments in their code.
- deltaonefour 5y agoI strictly use type annotations in python as if python was a typed language. This is within reason as typed languages exist.
- nickysielicki 5y agoThis bugged me, too. You can have a dictionary which maps to strings, you can have a list containing sets of frozensets of ints, but you can’t have a dictionary of unhashable types.
- mturmon 5y agoYep, here's how it plays out: Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin >>> d=dict() >>> l=[1,2,3] >>> d[l]=42 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'
- setr 5y agohttps://pypi.org/project/forbiddenfruit/ https://pypi.org/project/forbiddenfruit/ Apparently there are ways to monkey patch core types, so you could probably add a __hash__ method to list…
- bobbylarrybobby 5y agoWhile it's impossible for this type to exist in python, will a type checker complain if you write that type out? Does it have the notion that the first type of `Dict` (or just `dict` in Python 3.9+, iirc) must be `Hashable`