News for hackers. Stuff that matters.
Re:Free-threaded CPython is ready to experiment with by GTP on Friday July 12, 2024 @05:14PM (#40949506)
I don't get how this optional static typing works. I had a quick look at [1], and it begins with a note saying that Python's runtime doesn't enforce types, leaving the impression that you need to use third-party tools to do actual type checking. But then it continues just like Python does the check. Consider that I'm not a Python programmer, but the main reason I stay away from it is the lack of a proper type system. If this is going to change, I might reconsider it.

[1] https://docs.python.org/3/library/typing.html

Re:Free-threaded CPython is ready to experiment with by sveiss on Friday July 12, 2024 @05:21PM (#40949563)
The parser supports the type hint syntax, and the standard library provides various type hint related objects.

So you can do things like “from typing import Optional” to bring Optional into scope, and then annotate a function with -> Optional[int] to indicate it returns None or an int.

Unlike a system using special comments for type hints, the interpreter will complain if you make a typo in the word Optional or don’t bring it into scope.

But the interpreter doesn’t do anything else; if you actually return a string from that annotated function it won’t complain.

You need an external third party tool like MyPy or Pyre to consume the hint information and produce warnings.

In practice it’s quite usable, so long as you have CI enforcing the type system. You can gradually add types to an existing code base, and IDEs can use the hint information to support code navigation and error highlighting.

Re:Free-threaded CPython is ready to experiment with by quotemstr on Friday July 12, 2024 @11:48PM (#40951616)
> In practice it’s quite usable

It would be super helpful if the interpreter had a type-enforcing mode though. All the various external runtime enforcement packages leave something to be desired.

Re:Free-threaded CPython is ready to experiment with by setopt on Saturday July 13, 2024 @06:35AM (#40953151)
I agree. There are usable third-party runtime type checkers though. I like Beartype, which lets you add a decorator @beartype above any function or method, and it’ll complain at runtime if arguments or return values violate the type hints.

I think runtime type checking is in some ways a better fit for a highly dynamic language like Python than static type checking, although both are useful.

Re:Free-threaded CPython is ready to experiment with by nine_k on Friday July 12, 2024 @05:24PM (#40949588)
At MPOW most Python code is well-type-hinted, and mypy and pyright are very helpful at finding issues, and also for stuff like code completion and navigation, e.g. "go to the definition of the type of this variable".

Works pretty efficiently.

BTW, Typescript also does not enforce types at runtime. Heck, C++ does not enforce types at runtime either. It does not mean that their static typing systems don't help during at development time.

Re:Free-threaded CPython is ready to experiment with by GTP on Friday July 12, 2024 @05:43PM (#40949721)
> BTW, Typescript also does not enforce types at runtime. Heck, C++ does not enforce types at runtime either. It does not mean that their static typing systems don't help during at development time.

Speaking of C here as I don't have web development experience. The static type system does help, but in this case, it's the compiler doing the check at compile time to spare you many surprises at runtime. And it's part of the language's standard. Python itself doesn't do that. Good that you can use external tools, but I would prefer if this was part of Python's spec.

Edit: these days I'm thinking of having a look at Mojo, it seems to do what I would like from Python.

Re:Free-threaded CPython is ready to experiment with by kortex on Friday July 12, 2024 @08:58PM (#40950880)
https://github.com/python/mypy

> Python itself doesn't do that

The type syntax is python. MyPy is part of Python. It's maintained by the python foundation. Mypy is not part of CPython because modularity is good, the same way that ANSI C doesn't compile anything, that's what gcc, clang, etc are for.

Mojo is literally exactly the same way, the types are optional, and the tooling handles type checking and compilation.

Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:08PM (#40954834)
> Mojo is literally exactly the same way.

No, because in Mojo, type checking is part of the language specification: you need no external tool for that. Python defines a syntax that can be used for type checking, but you need an external tool to do that. GCC does type checking because it's defined in the language specification. You would have a situation analogous to Python only if you needed GCC + some other tool for type checking. This isn't the case.

Re:Free-threaded CPython is ready to experiment with by zo1 on Saturday July 13, 2024 @12:46PM (#40955126)
You're really splitting hairs here all to prove some sort of "type checking isn't included with python" property. Even if you're technically right, half the world really doesn't care and most code being churned out in Python is type-hinted, type-checked, and de-facto enforced at design time.

It's honestly winning the long-term war because traditional languages have really screwed things up with infinite and contrived language constructs and attempts just to satisfy some "language spec" and "compiler", whilst still trying to be expressive enough for what developers need and want to do safely. Python side-stepped all of that, has the perfect mix of type-checking and amazing "expressibility", and is currently proving that it's the way forward with no stopping it.

Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:58PM (#40955206)
I'm not saying that no one should use Python, I'm just saying why I don't like it. But if you instead like it I will not try to stop you using it.

This said, if most people use type hints and the proper tooling to enforce type checking, I would say this would be a good reason to properly integrate (optional) static typing in the language: it shows that most programmers like static typing. The problem I focused on in my example isn't the only advantage of a type system.

Re:Free-threaded CPython is ready to experiment with by zitterbewegung on Friday July 12, 2024 @05:21PM (#40949562)
Optional static typing is just like a comment (real term is annotation) of the input variable(s) and return variable(s). No optimization is performed. Using a tool such as mypy that kicks off on a CI/CD process technically enforces types but they are ignored by the interpreter unless you make a syntax error.
Re:Free-threaded CPython is ready to experiment with by nine_k on Friday July 12, 2024 @05:25PM (#40949593)
A language server in your IDE kicks in much earlier, and is even more helpful.
Re:Free-threaded CPython is ready to experiment with by zitterbewegung on Friday July 12, 2024 @08:39PM (#40950779)
I haven't used an IDE that has that but it is still just giving you a hint that there is an error and the interpreter is not throwing an error which was my point.
Re:Free-threaded CPython is ready to experiment with by kortex on Friday July 12, 2024 @09:05PM (#40950913)
That's true of most compiled languages. Unless we are talking about asserts, reflection, I think type erasure, and maybe a few other concepts, language runtimes don't check types. C does not check types at runtime. You compile it and then rely on control of invariants and data flow to keep everything on rails. In python, this is tricky because everything is behind at least one layer of indirection, and thus virtually everything is mutable, so it's hard to enforce total control of all data structures. But you can get really close with modern tooling.
Re:Free-threaded CPython is ready to experiment with by cwalv on Friday July 12, 2024 @10:43PM (#40951360)
>> and the interpreter is not throwing an error which was my point. > That's true of most compiled languages

True of most statically typed languages (usually no need to check at runtime), but not true in Python or other dynamically typed languages. Python would have been unusable for decades (prior to typehints) if that was true.

Re:Free-threaded CPython is ready to experiment with by kortex on Saturday July 13, 2024 @12:18PM (#40954912)
That's just reflection. That's a feature of code, not language runtime. I think there are some languages which in fact have type checking in the runtime as a bona-fide feature. Most won't, unless you do something like isinstance()
Re:Free-threaded CPython is ready to experiment with by setopt on Saturday July 13, 2024 @06:41AM (#40953186)
> I haven't used an IDE that has that

You don’t need an IDE for this, an LSP plugin + Pyright is sufficient to get live type checking. For instance, Emacs (Eglot), Vim (ALE), Sublime (SublimeLSP) all support Pyright with nearly no setup required.

Re:Free-threaded CPython is ready to experiment with by zo1 on Saturday July 13, 2024 @12:51PM (#40955155)
> "I haven't used an IDE that has that but it is still just giving you a hint that there is an error and the interpreter is not throwing an error which was my point."

At this point, I'm not sure how one is to take your opinion on this matter. Just like me coding some C# or Java in notepad and then opining to a Java developer audience about the state of their language and ecosystem.

Re:Free-threaded CPython is ready to experiment with by kortex on Friday July 12, 2024 @09:00PM (#40950888)
Nope. Type annotations can be executed and accessed by the runtime. That's how things like Pydantic, msgspec, etc, do runtime type enforcement and coercion.

There are also multiple compilers (mypyc, nuitka, others I forget) which take advantage of types to compile python to machine code.

Re:Free-threaded CPython is ready to experiment with by davepeck on Friday July 12, 2024 @05:20PM (#40949551)
Third party tools (mypy, pyright, etc) are expected to check types. cpython itself does not. This will run just fine:

python -c "x: int = 'not_an_int'"

My opinion is that with PEP 695 landing in Python 3.12, the type system itself is starting to feel robust.

These days, the python ecosystem's key packages all tend to have extensive type hints.

The type checkers are of varying quality; my experience is that pyright is fast and correct, while mypy (not having the backing of a Microsoft) is slower and lags on features a little bit -- for instance, mypy still hasn't finalized support for PEP 695 syntax.

Re:Free-threaded CPython is ready to experiment with by wk_end on Friday July 12, 2024 @05:18PM (#40949536)
The interpreter does not and probably never will check types. The annotations are treated as effectively meaningless at runtime. External tools like mypy can be run over your code and check them.
Re:Free-threaded CPython is ready to experiment with by cwalv on Friday July 12, 2024 @10:46PM (#40951375)
It checks types .. it doesn't check type annotations.

Just try:

  $ Python
  >>> 1 + '3'
Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @05:49PM (#40949760)
I think static typing is a waste of time, but given that you want it, I can see why you wouldn't want to use Python. Its type-checking is more half-baked and cumbersome than other languages, even TS.
Re:Free-threaded CPython is ready to experiment with by nine_k on Friday July 12, 2024 @06:47PM (#40950153)
I used to think like that until I tried.

There are areas where typing is more important: public interfaces. You don't have to make every piece of your program well-typed. But signatures of your public functions / methods matter a lot, and from them types of many internal things can be inferred.

If your code has a well-typed interface, it's pleasant to work with. If interfaces of the libraries you use are well-typed, you have easier time writing your code (that interacts with them). Eventually you type more and more code you write and alter, and keep reaping the benefits.

Re:Free-threaded CPython is ready to experiment with by simonw on Friday July 12, 2024 @06:55PM (#40950201)
This was the thing that started to bring me around to optional typing as well. It makes the most sense to me as a form of documentation - it's really useful to know what types are expected (and returned) by a Python function!

If that's baked into the code itself, your text editor can show inline information - which saves you from having to go and look at the documentation yourself.

I've started trying to add types to my libraries that expose a public API now. I think it's worth the extra effort just for the documentation benefit it provides.

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @06:59PM (#40950222)
This is what made me give it a shot in TS, but the problem is your types at interface boundaries tend to be annoyingly complex. The other problem is any project with optional types soon becomes a project with required types everywhere.

There might be more merit in widely-used public libraries, though. I don't make those.

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @06:49PM (#40950169)
I shouldn't have said it's a waste of time period, cause every project I work on does have static typing in two very important places: the RPC or web API (OpenAPI, gRPC, whatever it is), and the relational database. But not in the main JS or Py code. That's all I've ever needed.

I did try migrating a NodeJS backend to TS along with a teammate driving that effort. The type-checking never ended up catching any bugs, and the extra time we spent on that stuff could've gone into better testing instead. So it actually made things more dangerous.

Re:Free-threaded CPython is ready to experiment with by grumpyprole on Friday July 12, 2024 @07:32PM (#40950425)
A type checker is only going to add limited value if you don't put the effort in yourself. If everything string-like is just a string, and if data is not parsed into types that maintain invariants, then little is being constrained and there is little to "check". It becomes increasingly difficult the more sophisticated the type system is, but in some statically typed languages like Coq, clever programmers can literally prove the correctness of their program using the type system. Whereas a unit test can only prove the presence of bugs, not their absence.
Re:Free-threaded CPython is ready to experiment with by GTP on Friday July 12, 2024 @06:09PM (#40949916)
I instead think that the lack of static typing is a waste of time, since without it you can have programs that waste hours of computation due to an exception that would have been prevented by a proper type system ;)
Re:Free-threaded CPython is ready to experiment with by baq on Friday July 12, 2024 @05:59PM (#40949826)
Typescript is pretty much the gold standard, it’s amazing how much JavaScript madness you can work around just on the typechecking level.

IMHO Python should shamelessly steal as much typescript’s typing as possible. It’s tough since the Microsoft typescript team is apparently amazing at what they do so for now it’s a very fast moving target but some day…

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @06:45PM (#40950143)
Well the TS tooling is more painful in ways. It's not compatible with some stuff like the NodeJS profiler. Also there's some mess around modules vs "require" syntax that I don't understand fully but TS somehow plays a role.
Re:Free-threaded CPython is ready to experiment with by VeejayRampay on Friday July 12, 2024 @05:46PM (#40949744)
python will never be "properly typed"

what it has is "type hints" which is way to have richer integration with type checkers and your IDE, but will never offer more than that as is

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @05:50PM (#40949766)
It is properly typed: it has dynamic types :)
Re:Free-threaded CPython is ready to experiment with by GTP on Friday July 12, 2024 @06:05PM (#40949887)
Then we have very different ideas of what proper typing is :D Look at this function, can you tell me what it does?

  def plus(x, y):
    return x+y
If your answer is among the lines of "It returns the sum x and y" then I would ask you who said that x and y are numbers. If these are strings, it concatenates them. If instead you pass a string and a number, you will get a runtime exception. So not only you can't tell what a function does just by looking at it, you can't even know if the function is correct (in the sense that will not raise an exception).
Re:Free-threaded CPython is ready to experiment with by andrewaylett on Friday July 12, 2024 @06:17PM (#40949966)
It calls x.__add__(y).

Python types are strictly specified, but also dynamic. You don't need static types in order to have strict types, and indeed just because you've got static types (in TS, for example) doesn't mean you have strict types.

A Python string is always a string, nothing is going to magically turn it into a number just because it's a string representation of a number. The same (sadly) can't be said of Javascript.

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @07:06PM (#40950274)
Yeah and even with static typing, a string can be many things. Some people even wrap their strings into singleton structs to avoid something like sending a customerId string into a func that wants an orderId string, which I think is overkill. Same with int.
Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:21PM (#40954925)
> It calls x.__add__(y)

Your answer doesn't solve the problem, it just moves it: can you tell me what x. __add__(y) does?

Re:Free-threaded CPython is ready to experiment with by andrewaylett on Saturday July 13, 2024 @05:08PM (#40956899)
Whatever it's defined to do, and nothing else.

Dynamic typing, but strong typing.

There's no magic going on here, just an attribute lookup. It's still possible to write terrible Python code -- as it is in any language -- and the recommendation is still "don't write terrible code", just as it is in any language. You don't have to like it, but not liking it won't make it any different.

The older I get, the more I like writing statically-typed code. I wrote a lot more Python (for my own use) in my youth, and tend towards Rust nowadays. Speaking of which: if you dislike the dynamic typing of Python then you must hate the static typing of Rust -- what does

    fn add<T:Add<U>, U>(a: T, b: U) -> T::Output { a + b }
do?
Re:Free-threaded CPython is ready to experiment with by vhcr on Friday July 12, 2024 @07:08PM (#40950290)
In theory it's nice that the compiler would catch those kinds of problems, but in practice it doesn't matters.
Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:26PM (#40954965)
It can matter also in practice. Once I was trying some Python ML model to generate images. My script ran for 20 minutes to then terminate with an exception when it arrived at the point of saving the result to a file. The reason is that I wanted to concatenate a counter to the file name, but forgot to wrap the integer into a call to str(). 20 minutes wasted for an error that other languages would have spotted before running the script.
Re:Free-threaded CPython is ready to experiment with by cozzyd on Saturday July 13, 2024 @06:10PM (#40957278)
It's very hard to write long-running daemons in python partially for this reason, when you make a typo on a variable or method name in an uncommon code path.
Re:Free-threaded CPython is ready to experiment with by nequo on Friday July 12, 2024 @06:22PM (#40950001)
Both Haskell and OCaml can raise exceptions for you, yet most people would say that they are properly typed.

The plus function you wrote is not more confusing than any generic function in a language that supports that.

Re:Free-threaded CPython is ready to experiment with by hot_gril on Friday July 12, 2024 @06:16PM (#40949964)
When is the last time you had a bug IRL caused by passing the wrong kind of thing into plus(x, y), which your tests didn't catch?
Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:49PM (#40955145)
It never happened to me, because I don't use Python ;)

On a more serious note, your comment actually hints at an issue: unit testing is less effective without static type checking. Let's assume I would like to sum x and y. I can extensively test the function and see that it indeed correctly sums two numbers. But then I need to call the function somewhere in my code, and whether it will work as intended or not depends on the context in which the function is used. Sometimes the input you pass to a function depends from some external source outside your control, an if that's the case you have to resort to manual type checking. Or use a properly typed language.

Re:Free-threaded CPython is ready to experiment with by hot_gril on Saturday July 13, 2024 @01:05PM (#40955276)
This isn't an actual problem people encounter in unit testing, partially because you test your outer interfaces first. Also, irl static types often get so big and polymorphic that the context matters just as much.
Re:Free-threaded CPython is ready to experiment with by mixmastamyk on Friday July 12, 2024 @10:31PM (#40951304)
> you can't tell what a function does just by looking at it

You just did tell us what it does by looking at it, for the 90% case at least. It might be useful to throw two lists in there as well. Throw a custom object in there? It will work if you planned ahead with dunder add and radd. If not fix, implement, or roll back.

Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:30PM (#40954996)
> You just did tell us what it does by looking at it, for the 90% case at least

The problem is that you can't know if the function is going to do what you want it to do without also looking at the context in which it is used. And what you pass as input could be dependent on external factors that you don't control. So I prefer the languages that let me know what happens in 100% of the cases.

Re:Free-threaded CPython is ready to experiment with by mixmastamyk on Saturday July 13, 2024 @03:23PM (#40956216)
Protection from untrusted input is something that has to be considered in any language.

Not yet been a real world concern in my career, outside webforms, which are handled by framework.

Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @03:52PM (#40956415)
> Protection from untrusted input is something that has to be considered in any language

Sure, but some languages make it easier than others. And that was just one example, another example could be having a branch where the input to your function depends on some condition. You could have one of the two branches passing the wrong types, but you will only notice when that branch gets executed.

Re:Free-threaded CPython is ready to experiment with by radarsat1 on Saturday July 13, 2024 @04:14PM (#40956549)
And if you specify that they are numbers then you lose the ability of the function to generalize to vectors.

Indeed assuming it adds two things is correct, and knowing that concatenation is how Python defines adding strings is important for using the language in the intended way.

Re:Free-threaded CPython is ready to experiment with by GTP on Thursday July 18, 2024 @06:31AM (#40994233)
What about the case of passing a number and a string?
Re:Free-threaded CPython is ready to experiment with by infamia on Friday July 12, 2024 @08:36PM (#40950762)
> what it has is "type hints" which is way to have richer integration with type checkers and your IDE, but will never offer more than that as is

Python is strongly typed and it's interpreter is type aware of it's variables, so you're probably overreaching with that statement. Because Python's internals are type aware, it's how folks are able to create type checkers like mypy and pydantic both written in Python. Maybe you're thinking about TS/JSDoc, which is just window dressing for IDEs to display hints as you described?

Re:Free-threaded CPython is ready to experiment with by GTP on Saturday July 13, 2024 @12:18PM (#40954910)
I don't think you can say that a language is strongly typed if only the language's internals are. The Python interpreter prevents you from summing an integer to a string, but only at runtime when in many cases it's already too late. A strongly typed language would warn you much sooner.
Re:Free-threaded CPython is ready to experiment with by infamia on Saturday July 13, 2024 @05:09PM (#40956907)
Your example is bang on when describing a "strongly typed" language. That said, strongly typed is different from "static typing", which is what you described later in your post. Python is both strongly typed and dynamically typed. It is all rather confusing and just a big bowl of awful. I have to look up if I haven't referenced it in a while, because the names are far too similar and there aren't even good definitions around some of the concepts.

https://stackoverflow.com/questions/2690544/what-is-the-diff...

https://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic...

Re:Free-threaded CPython is ready to experiment with by kortex on Friday July 12, 2024 @09:10PM (#40950948)
s/will never be/already is/g

https://github.com/mypyc/mypyc

You can compile python to c. Right now. Compatibility with extensions still needs a bit of work. But you can write extremely strict python.

That's without getting into things like cython.

Re:Free-threaded CPython is ready to experiment with by NegativeK on Friday July 12, 2024 @09:04PM (#40950909)
Python's typing must accommodate Python's other goal as quick scripting language. The solution is to document the optional typing system as part of the language's spec and let other tools do the checking, if a user wants to use them.

The other tools are trivially easy to set up and run (or let your IDE run for you.) As in, one command to install, one command to run. It's an elegant compromise that brings something that's sorely needed to Python, and users will spend more time loading the typing spec in their browser than they will installing the type checker.