On Wed, 6 Sept 2023 at 18:24, John H Palmieri <jhpalmier...@gmail.com> wrote:
>
> For a while now, Python has allowed return types to be specified in 
> signatures:
>
>     def f() -> int:
>
> It is my understanding that this doesn't actually do any error-checking or 
> type-checking — see https://docs.python.org/3/library/typing.html. So this 
> works without error:
>
>     def f() -> float:
>         return "hello"
>
> This is cropping up in the Sage library. The file combinat/permutation.py has 
> several methods that claim (according to their signature) to return an 
> `Integer` but actually return an `int`, and this has led to at least one 
> person being confused (a recent post on stackexchange cited in the github 
> issue). Anyway, I've opened https://github.com/sagemath/sage/issues/36198 for 
> one of these instances, and we should be aware of this.

We have had this issue in SymPy as well. Contributors want to add the
type hints because they think it is good practice and many Python
editors even suggest these hints automatically now. The hints are
actually very useful for end users because editors can interpret them
and use them for autocomplete etc. I showed some examples here:
https://github.com/sympy/sympy/pull/25103

The hints are also useful for picking up bugs if used uniformly
throughout the codebase but it is difficult to get that benefit
incrementally in a large codebase with lots of legacy code.

The hints are worse than useless if they are not correct though.
Without any checking the hints are very likely to be incorrect. I
suggest disallowing hints unless there is actually a type-checker
running that will reject incorrect hints. When I enabled mypy in
SymPy's CI almost all of the work involved was fixing all of the
incorrect type hints that had already been added so I suggest not
allowing those to build up.

I also suggest choosing pyright over mypy if wanting to use a
typechecker because it is more principled and consistent (basically
just better AFAICT) and also pyright is what is used in all the
editors that are the main consumers of the hints anyway.

--
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/CAHVvXxQYy_T_S0GH%3DS1OCq%2BZEOQZ8Nu8U_h2%3DEvuNYYMF9Ot9A%40mail.gmail.com.

Reply via email to