News for hackers. Stuff that matters.
Re:Python is two languages now, and that's great by mikece on Monday February 27, 2023 @09:21AM (#34956674)
I get that the author is asserting that untyped Pythong is superior for infrastructure code but I don't understand why the author thinks this.
Re:Python is two languages now, and that's great by stu2b50 on Monday February 27, 2023 @10:54AM (#34957848)
I’m not sure I understand the “infrastracture” label, but certainly for the cases the author listed as “infra” typing can be cumbersome. Famously just json deserialization can be rather painful in statically typed languages, especially ones without complex compile time features.
Re:Python is two languages now, and that's great by Yoric on Monday February 27, 2023 @11:27AM (#34958317)
> Famously just json deserialization can be rather painful in statically typed languages, especially ones without complex compile time features.

That's funny, because last time I head to write json-related code in Python, I really missed Rust's serde, which makes deserialization + validation much easier to write in Rust than in Python.

YMMV, of course.

Re:Python is two languages now, and that's great by hewlett on Monday February 27, 2023 @12:18PM (#34959118)
Pydantic is that for Python
Re:Python is two languages now, and that's great by Yoric on Monday February 27, 2023 @01:02PM (#34959745)
Thanks, you remind me why I wanted to try Pydantic :)
Re:Python is two languages now, and that's great by marcosdumay on Monday February 27, 2023 @11:06AM (#34957993)
Hum... Almost all of the problems of serialization/deserialization in statically typed languages turn into testing problems in dynamically typed languages.

That's not a gain in any way.

Re:Python is two languages now, and that's great by stu2b50 on Monday February 27, 2023 @11:14AM (#34958123)
In this case it’s more about shifting where the static typing occurs, since the author is still arguing for a typed business layer.

Since I/O is usually untyped, you need to cram it into a typed schema somewhere. In strictly typed languages, that must occur upon ingestion. The author is saying you can delay it by having a layer on top of the I/O transform the data in untyped fashion before being typed later on.

Re:Python is two languages now, and that's great by Yoric on Monday February 27, 2023 @11:37AM (#34958482)
What's the benefit of delaying typing? By typing it at ingestion, you guarantee that invalid (with respect to structure) untrusted data will not break your code down the line. Doing otherwise means that you're manipulating untrusted data with a strong risk of forgetting that it is untrusted.

I have seen large applications (think hundreds of millions of daily users) break because of such oopses.

Re:Python is two languages now, and that's great by eropple on Monday February 27, 2023 @11:02AM (#34957940)
Serialization/deserialization can be really gross in something like Java, but TypeScript gives you some really good tools (zod, typebox, etc.) to validate and type-guard your inputs that I'm sure could be implemented in Python--if they're not already.
Re:Python is two languages now, and that's great by amarant on Monday February 27, 2023 @11:38AM (#34958505)
If serialisation is painful in Java, you're doing it wrong.

Libraries like Jackson make it entirely painless.

I would assume JSON parsing is a solved problem in any typed language.

Re:Python is two languages now, and that's great by eropple on Monday February 27, 2023 @12:13PM (#34959039)
I've written a lot of code using Jackson and I strongly disagree that it's a solved problem. If you're Java on both ends, it's probably fine, but then your serde style is an implementation detail. And that's great, but if you're not, you are in a bear pit. The lingua franca for JSON specification is JSON Schema, and there is significant impedance mismatch between the standard JSON tools folks use in Java and with JSON Schema as used by most other folks. (Jackson has a nice JSON Schema module for exporting it, but importing/codegen is pretty gross.)
Re:Python is two languages now, and that's great by pbalau on Monday February 27, 2023 @11:07AM (#34958018)
Pydantic will do that for Python.
Re:Python is two languages now, and that's great by eropple on Monday February 27, 2023 @12:18PM (#34959103)
Yeah, I assumed as much. I've only looked at Pydantic in the context of FastAPI, as Python's not really my thing, but it looks really well-thought-out.
Re:Python is two languages now, and that's great by sixbrx on Monday February 27, 2023 @11:42AM (#34958572)
I find Java pretty nice for handling JSON serialization/deserialization these days. Just define some records for the expected structure, and Jackson's object mapper handles the rest.
Re:Python is two languages now, and that's great by eropple on Monday February 27, 2023 @12:14PM (#34959052)
It's the "defining some records" part that's slow and gritty. JSON is usually an interchange format, and Java class construction is either slow, tedious, and manual, or involves massaging a code generator (which, at least the last time I checked a few months ago, weren't very good in Java).
Re:Python is two languages now, and that's great by vips7L on Monday February 27, 2023 @12:23PM (#34959193)
I don’t see how declaring a data model class is anymore difficult in Java than another typed language.

  record Thing(String name, int size, LocalDate when) {}
The only way I could see this being easier is to forgo type safety completely.
Re:Python is two languages now, and that's great by eropple on Monday February 27, 2023 @01:55PM (#34960427)
You're assuming you only have to define it in one language. JSON is an interchange format. The canonical way to express it is JSON Schema, and the tools are unpleasant in Java. So is dealing with structural typing of polymorphic responses. In an ideal world we wouldn't have those, but we live in this one.

These things are just better when you aren't in a nominatively typed language with some pretty crusty assumptions.

Re:Python is two languages now, and that's great by pjc50 on Monday February 27, 2023 @10:58AM (#34957900)
I think from the perspective that "infrastructure" code being that which rummages around in objects, where the only distinction that matters being dict vs primitive.
Re:Python is two languages now, and that's great by animuchan on Monday February 27, 2023 @11:04AM (#34957961)
Yup! For me it's precisely the opposite of that, the deeper in the systems code, the more types I write. Same with JS, the bulk of libraries IMO should be written in TypeScript.
Re:Python is two languages now, and that's great by mandevil on Monday February 27, 2023 @11:11AM (#34958066)
Not the writer, but based on what the author wrote, I think it's based on the author's experience with Java. It can be a hideous nightmare of java.util.Date versus java.sql.Date versus java.time.LocalDate (just to pick one very well known fiasco in the language- Java defenders please don't come after me) and all of them have to exist to avoid breaking things but you have to have different ways to handle each of them, so dealing at the boundaries between business logic and "infrastructure" is always a mess of converting types- oh this library is old enough that it needs a utilDate, so I need to convert it back down, then convert it back up at every boundary.

By contrast, duck-typing means that the intricacies of which exact type you are deserializing into is a lot less important in python.

Re:Python is two languages now, and that's great by shellac on Monday February 27, 2023 @11:40AM (#34958529)
Might not be the best example since Python made quite a mess of dates and date times (in fairness, like most languages), and duck typing didn't help at all.
Re:Python is two languages now, and that's great by mharig on Wednesday March 01, 2023 @07:20AM (#34981545)
No. The people make the mess. For persistence, only UTC without timezone makes sense. For UI, with few exceptions, only local time without time zone in ISO 8601 makes sense.

The only not so easy decision is: is it better to store as Unix epoch or as ISO string. That is a trade of between compactness and easy searching of date/time ranges.

Re:Python is two languages now, and that's great by woeirua on Monday February 27, 2023 @11:27AM (#34958321)
> By contrast, duck-typing means that the intricacies of which exact type you are deserializing into is a lot less important in python.

How do you explain the popularity of type hinting in Python then? Type hinting arose _because_ duck typing is horrible for large systems.

Re:Python is two languages now, and that's great by mandevil on Monday February 27, 2023 @11:30AM (#34958361)
Well, the article is the author's attempt to answer that question. I'm not sure that it's convincing to me, but then again I have a preference for strongly-typed languages so I'm probably not the correct audience. I'm just trying to help someone else understand the author's argument, as I understand it.
Re:Python is two languages now, and that's great by paganel on Monday February 27, 2023 @11:56AM (#34958776)
Not the OP but I explain it as former Java/C# programmers coming to Python for one reason or another.

I’ve come to Python 20 years ago after a short stint with PHP and I don’t really care if the incoming stuff I have to handle is a list or a tuple (let’s say) as long as it quacks and walks like the proverbial duck.

More generally, you could say Python the original language is the victim of its own success (the same goes for JavaScript in relation to TypeSctipt).

Re:Python is two languages now, and that's great by jjoonathan on Monday February 27, 2023 @11:25AM (#34958282)
Duck Typing always struck me as utterly absurd wishful thinking. Two different types built for the same task will almost certainly not happen to be API compatible.

Am I missing something?

Re:Python is two languages now, and that's great by mandevil on Monday February 27, 2023 @11:33AM (#34958418)
I have bounced back and forth between typed and untyped languages in my two decades of employment history, and I prefer typed languages myself, even when I've been writing libraries nested deep in what I think the author would describe as the "infrastructure layer," but this is what I believe the original author of the linked piece is arguing.
Re:Python is two languages now, and that's great by moron4hire on Monday February 27, 2023 @11:40AM (#34958527)
* * *
Re:Python is two languages now, and that's great by hobs on Monday February 27, 2023 @11:27AM (#34958314)
No, in my experience you are trading off ease of coding for order of magnitude more production bugs.
Re:Python is two languages now, and that's great by mharig on Wednesday March 01, 2023 @07:09AM (#34981441)
So you do not write tests?
Re:Python is two languages now, and that's great by hobs on Tuesday March 07, 2023 @08:50AM (#35055273)
I absolutely do, and one of those effective tests is a simple type annotation.
Re:Python is two languages now, and that's great by jcranmer on Monday February 27, 2023 @12:16PM (#34959070)
There are cases where it can be useful. One thing that comes to mind is numerical code, where your algorithms are generic over things that have *,/,+,-,==,!=, conversion from 0 or 1 [or some subset thereof] do the "right thing". Trying to define proper interface types for what constitutes the "right thing" is somewhat tedious without much relative gain.
Re:Python is two languages now, and that's great by alanwreath on Monday February 27, 2023 @02:54PM (#34961054)
Its worked for us when dealing with local operations vs. cloud bucket operations. Protocols also help when you really don’t care to litter your code with some vendor specific object but instead just declare what functionality should be expected from the object passed in. It helps describe what you’re doing with it anyway, it’s why typing with Iterable can be helpful if you don’t care if it’s a list, set, or tuple.
Re:Python is two languages now, and that's great by martinflack on Tuesday February 28, 2023 @12:27AM (#34965973)
In sequences, even just too calls like "more?" and "next" will let you entertain all kinds of strict and lazy possibilities.
Re:Python is two languages now, and that's great by sdfghswe on Monday February 27, 2023 @03:20PM (#34961366)
It's funny that you typoed Python as Pythong. I do that a lot - in my case I know it's faulty muscle memory, but I don't know where it comes from.
Re:Python is two languages now, and that's great by mikece on Monday February 27, 2023 @04:57PM (#34962480)
And to my chagrin I didn't realize it until AFTER the time window for editing a comment passed. Oh well.
Re:Python is two languages now, and that's great by Spivak on Monday February 27, 2023 @03:22PM (#34961397)
I think it would have been better to say "internal library code for foundational projects." That code that takes advantage of all the freedom in Python to mess with types, generate classes on the fly, use type annotations at runtime, add and remove attributes dynamically. Code that is basically a notch away from `eval` (or sometimes literately for dataclasses). There's no way you'll ever be able to fully type this stuff outside of Any Any Any Any. But to be useful, the boundary between your library and it's user should ideally be fully typed so the type checker can help your users use the library correctly.