News for hackers. Stuff that matters.
Re:Show HN: I Wrote a Book on Java by cbsmith on Tuesday September 24, 2024 @01:52PM (#41639140)

    class X(TypedDict):
        x: str

    class Y(TypedDict):
        y: str

    class XYZ(X, Y):
        z: str
That should get you the class/superclass relationship that you want, no?
Re:Show HN: I Wrote a Book on Java by maleldil on Tuesday September 24, 2024 @04:39PM (#41640677)
That needs to be explicit for any interacting types. You must define separate classes and explicitly define their hierarchy. This is fine if you control all the types, but it breaks down quickly. The best example is having two TypedDicts with the same members; in Python, you cannot use one instead of the other.

    from typing import TypedDict

    class A(TypedDict):
        a: int
        b: str
        

    class B(TypedDict):
        a: int
        b: str
        
    def f(a: A) -> None: pass

    b = B(a=1, b='b')
    f(B) # mypy error: Argument 1 to "f" has incompatible type "type[B]"; expected "A"  [arg-type]
On the other hand, this is legal in Typescript:

    interface A {
      a: number;
      b: string;
    }

    interface B {
      a: number;
      b: string;
    }  

    function f(a: A) {}

    const b: B = {a: 1, b: 'b'};
    f(b);
This is most useful when A has a subset of B's attributes, like this (which also doesn't work in Python):

    interface A {
      a: number;
    }

    interface B {
      a: number;
      b: string;
    }  

    function f(a: A) {}

    const b: B = {a: 1, b: 'b'};
    f(b);
Re:Show HN: I Wrote a Book on Java by cbsmith on Tuesday September 24, 2024 @06:36PM (#41641699)
That seems a lot like duck typing to me.
Re:Show HN: I Wrote a Book on Java by maleldil on Wednesday September 25, 2024 @04:00AM (#41644915)
Yes, it is. Typed Python supports duck typing to some extent; see typing.Protocol and stuff like Sequence, Iterable, Mapping, etc.
Re:Show HN: I Wrote a Book on Java by cbsmith on Wednesday September 25, 2024 @12:53PM (#41649378)
I'd argue non-typed Python supports duck typing pretty well too, so you don't necessarily need Typed Python to support it.