I'm a heavy user of type hints and enable pyright and mypy's strict modes whenever possible. However, you can't always be strict: if you use almost any package in the data science/ML ecosystem, you're unlikely to get good type inference and checking[1]. In those cases, it can still be useful to type some parameters and return values to benefit from _some_ checking, even if you don't have 100% coverage. Type hints als…
> There are stubs for pandas, but they're not enough because pandas has a tendency to change return types based on the input, and that breaks quickly. A mechanism like Haskell's type application seems like it could solve at least most, maybe all of those problems.
The different uses of Python type hints
41–50 of 70 posts
Re: The different uses of Python type hints
#42The only reason to consider type hints is for a performance increase and there wasn't any mention of that. What can you really expect from using type hints accurately?
> Mypyc compiles Python modules to C extensions. It uses standard Python type hints to generate fast code. Mypyc uses mypy to perform type checking and type inference.
> Mypyc can compile anything from one module to an entire codebase. The mypy project has been using mypyc to compile mypy since 2019, giving it a 4x performance boost over regular Python.
I have not experience a 4x boost, rather between 1.5x and 2x. I guess it depends on the code.
Re: The different uses of Python type hints
#43I remember that it felt rough. I had issues specially with funcions using veriadic types in generics. I also remember having issues with overloading a function: sometimes it would go for the more generic one, instead of going for the more specific one when inferring types.
I managed to solve all of that. Unfortunately, that happened some time ago and I don't remember the specifics, only that it was a fun project to develop. I use it frequently in other projects.
Re: The different uses of Python type hints
#44The 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.
If this sounds fun then you can go play with e.g idris or F*.
Re: The different uses of Python type hints
#45A 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…
> To add overloaded implementations to the function, use the register() attribute of the generic function, which can be used as a decorator. For functions annotated with types, the decorator will infer the type of the first argument automatically:
That appears to be the only other case.
Re: The different uses of Python type hints
#46I couldn't do unit tests all at once because of the design not having any way of accommodating them. I needed something to stop the enormous waste of time.
So I added type hints. The IDE should show me when illogical things were being done with parameters to methods/functions. It was fairly quick compared to a total refactoring but not effort free. I barely noticed any effect. Didn't catch a single error. Eventually I created a kind of dummy version of Android that "built" in a few seconds and I tested against that first. That allowed me to speed up changes and get some refactoring done to make a few critical unit tests and the whole thing started to get under control.
This anecdote has almost no meaning - you cannot conclude that type hints have no benefit because of one case - I just think that tests are almost always more important and hinting and the whole rigmarole of strong typing are much less of a panacea than tests are.
Re: The different uses of Python type hints
#47Ex: Programmers value types much more for documentation vs preventing bugs. I had not expected that answer!
Re: The different uses of Python type hints
#48Earlier quoted context omitted.
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.
Could you show some examples?
Re: The different uses of Python type hints
#49A 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…
The Annotated type is worth mentioning as well. Today in something like SQLModel you do (from the readme): class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) And that's fine. I wouldn't necessarily change anything here. Annotated gives you the option of approaching things in a different way, though. class Hero(SQLModel, table=True): id: PrimaryKey[Optional[int]] = None I have…
Re: The different uses of Python type hints
#50A 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')