News for hackers. Stuff that matters.
Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @01:22PM (#36329261)
Doesn't support algebraic data types, though.

FWIW I think this article mentioning them at all is sort of weird because the article doesn't propose them either, lmao

Re:Proposing a Struct Syntax for Python by tikhonj on Wednesday June 14, 2023 @01:32PM (#36329411)
Right, the article talks about "type Point = Point1D | Point2D | Point3D" syntax at the end... which would work just as well with dataclasses as with structs!
Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @03:53PM (#36331637)
It already does too. Just remove the `type` bit and you have an alias for a union, which has been supported since typing was added (though the `|` syntax was only added in 3.10). And AFAIK the type checkers do handle completeness on unions.
Re:Proposing a Struct Syntax for Python by fritzo on Wednesday June 14, 2023 @02:12PM (#36330091)
Do you mean Python's lack of a labeled union type?
Re:Proposing a Struct Syntax for Python by Spivak on Wednesday June 14, 2023 @02:30PM (#36330416)
Pydantic does though.

    class A(BaseModel):
      status: Literal["success"]

    class B(BaseModel):
      status: Literal["error"]

    class C(BaseModel, ABC):
      __root__: Union[A, B] = Field(discriminator="status")

    aorb = C.parse_obj(blah…).__root__

    if aorb.status == "error":
      # aorb will type check as B.
Re:Proposing a Struct Syntax for Python by b33j0r on Wednesday June 14, 2023 @03:16PM (#36331097)
You are a friend and a scholar.

I was looking for a solution in pydantic that is wildly relevant. Thanks, I didn’t realize that was implemented/existed.

My only objection is using ABC in your MRO. Haha, just a little bit overloaded with abstract base classes and using A, B, and C.

I got it eventually! Cool.

Re:Proposing a Struct Syntax for Python by Spivak on Wednesday June 14, 2023 @04:28PM (#36332130)
Ya know what, on reading that that does seem confusing. My brain had long since internalized ABC as a single token I didn't even notice.
Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:32PM (#36332177)
I believe this is a good example of type narrowing :)
Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:31PM (#36332170)
Yeah. Been spoiled by Rust.
Re:Proposing a Struct Syntax for Python by contravariant on Wednesday June 14, 2023 @04:49PM (#36332435)
It doesn't seem that far off though, it does have a union type and with pattern matching you can achieve pretty much the same thing (sure you may need a few extra classes if the types aren't distinct already but that seems like a small difference).

I don't know if type checkers can figure out if a pattern match statement is exhaustive yet though, that would be interesting.

Re:Proposing a Struct Syntax for Python by joiguru on Wednesday June 14, 2023 @02:29PM (#36330392)
Which Python 3.10 and static type checkers much of the benefits of algebraic data types is already available [1].

[1]: https://stackoverflow.com/questions/16258553/how-can-i-defin...

Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @03:50PM (#36331589)
The typed equivalent of a sum type in python is a union (https://docs.python.org/3/library/typing.html#typing.Union). Works with pretty much every type you can think of, but has the usual union limitation that you can't have two variants of the same type (e.g. int | int | float).

Then again, you can just create newtype wrappers, and that's essentially the same thing as different constructors for a sum type.

Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:32PM (#36332191)
You'd need to create newtype wrappers anyway in order to tell them apart, I'd assume. Otherwise it wouldn't really be an algebraic data type as much as a... union type.
Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @04:36PM (#36332235)
Hence “equivalent”. Of your variants all have trivial and non-overlapping payloads you can just use them as-is, or erlang-style tagged tuples, in most cases it’ll do fine.