Live data from Hacker News

The different uses of Python type hints

lukeplant.me.uk

21–30 of 70 posts

Re: The different uses of Python type hints

#21

A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses]( https://docs.python.org/3/library/dataclasses.html#module-da... ) Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and i…

> Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. FWIW, `typing.NamedTuple` did this in Python 3.5, three years before dataclasses was introduced in 3.7. class Foo(typing.NamedTuple): a: int b: str f = Foo(a=1, b="hello") print(f.b) # "hello"

Great catch, how could I forget!

Re: The different uses of Python type hints

#22

A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses]( https://docs.python.org/3/library/dataclasses.html#module-da... ) Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and i…

Protocols with @runtime_checkable fit your description!

Re: The different uses of Python type hints

#23
post #7

The issue that I have with Python type hints is they they don't go nearly far enough in describing the data being manipulated. Specifically, I'm thinking of stuff like the dimensionality and cardinality of Numpy arrays or Pandas frames. Usually that's the stuff where I have most questions when I look at Python code and the type system as it's being used now offers no help there.

You want dependent types!

Python can actually do (some) dependent types with generics but it's not pretty.

The only real use-case that is both possible and worthwhile I've found is being able to say a value is a T if there's a default and an Optional[T] otherwise.

Re: The different uses of Python type hints

#24

A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses]( https://docs.python.org/3/library/dataclasses.html#module-da... ) Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and i…

Dataclasses, like typing.NamedTuple, do not care about type hints:

    >>> @dataclasses.dataclass
    ... class D:
    ...   x: int
    ...
    >>> D('a')
    D(x='a')

Re: The different uses of Python type hints

#25

A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses]( https://docs.python.org/3/library/dataclasses.html#module-da... ) Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and i…

Dataclasses, like typing.NamedTuple, do not care about type hints: >>> @dataclasses.dataclass ... class D: ... x: int ... >>> D('a') D(x='a')

The idea with type hints in Python though is that they’re meant to be checked using some static analysis tool like mypy/pyright/etc. The runtime behavior for the most part remains unchanged in the sense that the Python interpreter won’t enforce the types in cases such as the one you’ve provided.

Re: The different uses of Python type hints

#26

A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses]( https://docs.python.org/3/library/dataclasses.html#module-da... ) Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and i…

Dataclasses, like typing.NamedTuple, do not care about type hints: >>> @dataclasses.dataclass ... class D: ... x: int ... >>> D('a') D(x='a')

Dataclasses do care about type hints in some cases, for example when determining what counts as a field.

    @dataclass
    class A:
        a: int = 0
        b = 1

    >>> A(a=1, b=2)
    TypeError: __init__() got an unexpected keyword argument 'b'

Re: The different uses of Python type hints

#28
post #3

This sort of thing is why I gave up Python. I could see having strong typing. Or optional strong typing. But unchecked type hints are just silly. The way everybody else seems to be going is strong typing at function interfaces, with automatic inference of as much else as can be done easily. C++ (since "auto"), Go, Rust, etc.

With python you can have your cake and it eat it, though.

Mock up something fast, no type hints.

Now, take that POC and make it production ready, by using mypy and pydantic.

Re: The different uses of Python type hints

#29
post #7

The issue that I have with Python type hints is they they don't go nearly far enough in describing the data being manipulated. Specifically, I'm thinking of stuff like the dimensionality and cardinality of Numpy arrays or Pandas frames. Usually that's the stuff where I have most questions when I look at Python code and the type system as it's being used now offers no help there.

Create a class what enforces this and use the new class as a type?

Re: The different uses of Python type hints

#30
post #7

The issue that I have with Python type hints is they they don't go nearly far enough in describing the data being manipulated. Specifically, I'm thinking of stuff like the dimensionality and cardinality of Numpy arrays or Pandas frames. Usually that's the stuff where I have most questions when I look at Python code and the type system as it's being used now offers no help there.

Create a class what enforces this and use the new class as a type?

The trouble is that's not how any of the ML or data science Python code is written at the moment. Such practices could help although I think more elegant solutions should be explored.
Post reply on HN