News for hackers. Stuff that matters.
Re:Python 3 Types in the Wild by ghostwriter on Friday March 18, 2022 @10:36AM (#30722809)
> which allow developers to say that a function returns Dict[List[Set[FrozenSet[int]]], str]

this type is impossible in Python

Re:Python 3 Types in the Wild by nickysielicki on Friday March 18, 2022 @11:19AM (#30723395)
This 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.

Re:Python 3 Types in the Wild by mturmon on Friday March 18, 2022 @11:47AM (#30723732)
Yep, 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'
Re:Python 3 Types in the Wild by setr on Friday March 18, 2022 @01:03PM (#30724692)
https://pypi.org/project/forbiddenfruit/

Apparently there are ways to monkey patch core types, so you could probably add a __hash__ method to list…

Re:Python 3 Types in the Wild by bobbylarrybobby on Friday March 18, 2022 @12:50PM (#30724543)
While 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`
Re:Python 3 Types in the Wild by mivade on Friday March 18, 2022 @10:46AM (#30722947)
Correct. Keys must be immutable and hashable.
Re:Python 3 Types in the Wild by pdonis on Friday March 18, 2022 @11:11AM (#30723307)
Technically 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.
Re:Python 3 Types in the Wild by lolive on Friday March 18, 2022 @11:01AM (#30723165)
With this type system is it possible to subclass string and int, to enforce types for, let’s say, firstName, postalCode, etc.
Re:Python 3 Types in the Wild by ghostwriter on Friday March 18, 2022 @12:06PM (#30723978)
yes, but the current way of defining it [1] is not very elegant:

    FirstName  = NewType('FirstName', str)
    PostalCode = NewType('PostalCode', str)

[1] https://docs.python.org/3/library/typing.html#newtype
Re:Python 3 Types in the Wild by paisawalla on Friday March 18, 2022 @11:57AM (#30723849)
Yes, simply

    FirstNameType = str
(But don't use ints for postal codes, even if the postal codes are numeric)
Re:Python 3 Types in the Wild by lolive on Friday March 18, 2022 @12:00PM (#30723885)
Huuum. 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?
Re:Python 3 Types in the Wild by jamincan on Friday March 18, 2022 @02:03PM (#30725485)
Yeah, 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
Re:Python 3 Types in the Wild by paisawalla on Friday March 18, 2022 @06:11PM (#30728195)
In 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.
Re:Python 3 Types in the Wild by colejohnson66 on Friday March 18, 2022 @01:41PM (#30725194)
> ... 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})/)

Re:Python 3 Types in the Wild by kennu on Saturday March 19, 2022 @11:34AM (#30734601)
And 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.
Re:Python 3 Types in the Wild by lolive on Friday March 18, 2022 @11:06AM (#30723226)
Does IntelliJ use any kind of static type analysis to help the user autocomplete properly?

[== do we have something like Java???]

Re:Python 3 Types in the Wild by deltaonefour on Friday March 18, 2022 @11:52AM (#30723789)
Yes. Try pycharm.
Re:Python 3 Types in the Wild by aenis on Friday March 18, 2022 @12:10PM (#30724015)
Within 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.

Re:Python 3 Types in the Wild by deltaonefour on Friday March 18, 2022 @03:14PM (#30726315)
I strictly use type annotations in python as if python was a typed language. This is within reason as typed languages exist.
Re:Python 3 Types in the Wild by gnulinux on Friday March 18, 2022 @10:52AM (#30723033)
Not only impossible in python, but also seems conceptually useless (why would you have a set of frozen sets?).
Re:Python 3 Types in the Wild by raymondh on Friday March 18, 2022 @11:42AM (#30723657)
Almost 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.
Re:Python 3 Types in the Wild by deltaonefour on Friday March 18, 2022 @11:42AM (#30723658)
This 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.

Re:Python 3 Types in the Wild by yifanl on Friday March 18, 2022 @11:39AM (#30723633)
Ensure 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?)

Re:Python 3 Types in the Wild by nisegami on Friday March 18, 2022 @12:13PM (#30724073)
I guarantee something like that is running in production somewhere in the world (maybe without the list part).