News for hackers. Stuff that matters.
Re:Writing Python like it's Rust by hakanderyal on Sunday May 21, 2023 @04:56AM (#36019084)
I went through a similar journey, without the Rust part. Started using type hints, data classes, pydantic to get the benefit of static typing after dealing with the pain of refactoring dynamically typed projects.

It was better, but it _feels_ like lipstick on a pig. I love Python, but types are not what it's best at. It's missing features that makes typing easier.

I've realized if was going to write typed Python, I might as well switch to one that has better support for that and switched to .NET.

I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. Expressions and extension methods, reflection with `nameOf` opens up a lot of possibilities.

One thing that kept me using Python was SqlAlchemy, an ORM, a damn good one. Entity Framework Core, after V6 is a better one, especially with LINQ.

If you have the opportunity, give it a try. You might be surprised.

Re:Writing Python like it's Rust by tejinderss on Sunday May 21, 2023 @05:32AM (#36019252)
> I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way.

If you like flexibility with a good type system, give Typescript a try, its miles ahead of mypy and its designed by same person who’s behind C#.

Re:Writing Python like it's Rust by AlexSW on Sunday May 21, 2023 @05:50AM (#36019347)
I don't think this is a completely accurate comparison, as TypeScript is transpiled to JavaScript and this provides restrictions to the expressibility and usability of types at runtime, which I've sometimes hit my head against. C# has much more type information available at run time, as does Python.
Re:Writing Python like it's Rust by rtpg on Sunday May 21, 2023 @06:03AM (#36019416)
Typescript is of course nice and is a great type system but can’t totally paper over the absolutely impoverished base language that is JS. When you use Python and things like equality do what almost anyone wants instead of checking object identity, it’s real annoying to futz around in TS
Re:Writing Python like it's Rust by influxmoment on Sunday May 21, 2023 @06:36AM (#36019581)
[1, 3, 12].sort() == [1, 12, 3]

5 in [5] == false

Re:Writing Python like it's Rust by ptx on Sunday May 21, 2023 @09:14AM (#36020547)
Even setting the sort order aside, just the fact that it both returns the sorted array and also sorts it in-place is not great. I prefer Python's distinction between sorted (returning a sorted copy) and list.sort (sorting in place and returning nothing).
Re:Writing Python like it's Rust by grumpyprole on Sunday May 21, 2023 @09:41AM (#36020757)
Even better would be to try F#, it will be able to use all the .NET libraries, has Python's succinctness, but with a strong type system (and inference).
Re:Writing Python like it's Rust by hakanderyal on Sunday May 21, 2023 @04:39PM (#36024499)
I may switch to it eventually, but at this point in my career 16 years in, I try to avoid seemingly niche things when I just want to get stuff done. "Boring" is good. I know it's the same platform under the hood but I decided to spend my "innovation tokens"[0] on the stuff I'm buildin.

[0]: https://mcfunley.com/choose-boring-technology

Re:Writing Python like it's Rust by grumpyprole on Sunday May 21, 2023 @04:56PM (#36024612)
I call it "novelty budget" and I completely agree.
Re:Writing Python like it's Rust by hakanderyal on Sunday May 21, 2023 @04:31PM (#36024433)
I'm a full stack dev so I'm also using TS at the same time. While the flexibility is nice, it's too flexible for my taste. To give a one basic example, I want to go to the definition of a symbol with a single action. Not possible with the current TS tooling and all the crazy things you can do with it's type system.

I want the type system to be predictable, so it can support whatever I want to build on top of.

There are features I'm missing with C#, but still I'll choose it over TS any day.

Re:Writing Python like it's Rust by JodieBenitez on Sunday May 21, 2023 @05:32AM (#36019249)
> One thing that kept me using [language X] was [insert awesome library here]

And that's why I keep [language X], because sometimes the equivalent in [language Z] is not as good.

Re:Writing Python like it's Rust by Kwpolska on Sunday May 21, 2023 @05:36AM (#36019273)
If [awesome library] is the primary thing your code does, then sure, you might be stuck with [language X]. But if it isn’t, then perhaps the [language Z] replacement might be good enough (or better but not yet well understood, as in the case of the OP), or you might keep [language X] just for that small component.
Re:Writing Python like it's Rust by Kwpolska on Sunday May 21, 2023 @05:33AM (#36019256)
Seconded, C# is a great language these days. LINQ is just magic (in a good sense of the word), doubly so when it’s seamlessly translated into SQL by Entity Framework.
Re:Writing Python like it's Rust by NeutralForest on Sunday May 21, 2023 @05:56AM (#36019375)
A typical pain point where type hinting fails in Python is with list of some types, it can't be enforced by the language without more checks.

For example, you can't do:

if isinstance(x, list[int])

though it would be super useful

Re:Writing Python like it's Rust by mejutoco on Sunday May 21, 2023 @08:53AM (#36020400)
You need to use mypy or similar to not check it at runtime:

    els: List[int]
To test it at runtime you can use typeguard s @typechecked annotation. For convenience you can also use Pydantic.
Re:Writing Python like it's Rust by NeutralForest on Sunday May 21, 2023 @10:31AM (#36021256)
Yes I know, I'm just pointing out that you need additional tool to check that.
Re:Writing Python like it's Rust by smnrchrds on Sunday May 21, 2023 @03:44PM (#36024127)
I didn't know about typeguard. Thank you for the recommendation!

How do you do this in Pydantic though?

Re:Writing Python like it's Rust by mejutoco on Sunday May 21, 2023 @05:52PM (#36025012)
For this example pydantic would be overkill, but you can use with it class definitions that follow standard python class hints, with additional utilities like YourClass.parse_obj(d) that parse a dict and throw a validation error if it does not match, with mostly standard type annotations.

It can also be used to generate openapi swagger files from the types and things like that.

Re:Writing Python like it's Rust by Yajirobe on Sunday May 21, 2023 @06:19AM (#36019504)
What can .NET do that Python+mypy cannot?
Re:Writing Python like it's Rust by ptx on Sunday May 21, 2023 @06:38AM (#36019592)
Another difference you might be surprised by is that the .NET tooling by default collects various data from your system and sends it to Microsoft [1].

If you want to avoid this (and still want to use .NET) you'll have to make sure that the environment variable DOTNET_CLI_TELEMETRY_OPTOUT is set in all contexts before touching anything.

[1] https://github.com/dotnet/sdk/issues/6145

Re:Writing Python like it's Rust by Sosh101 on Sunday May 21, 2023 @08:27AM (#36020201)
> DOTNET_CLI_TELEMETRY_OPTOUT

I didn't know about this. I wonder how that sits legally with local data protection rules (EU).

Re:Writing Python like it's Rust by jeroenhd on Sunday May 21, 2023 @03:57PM (#36024198)
I doubt they way they approach it is legal, but I don't think any of the DPAs have time to look into it.

For what it's worth, the tool prints a clear warning the first time you invoke it. You shouldn't need to opt out regardless, but they do at least communicate their stalking.

Re:Writing Python like it's Rust by DangitBobby on Sunday May 21, 2023 @01:00PM (#36022781)
Django has kept me chained to Python. Just can't beat admin/migrations/ORM. Ive often wondered why a true django-like project has never been born out of the JavaScript/Typescript ecosystem.
Re:Writing Python like it's Rust by lyu07282 on Sunday May 21, 2023 @03:56PM (#36024196)
I'm also 100% convinced most people who use mypy don't realize the myriad ways it just silently stopps typing things or just silently crashes with a 0 exit code. Even if you configure it to warn untyped functions etc. It will still just not work properly in some of circumstances and you will literally never know until you debug a bug that just happened to trigger it. There are over 1.4k open bug tickets it's such a broken piece of software: https://github.com/python/mypy/issues?q=is%3Aissue+is%3Aopen...

The involvement of Guido in mypy is such a tragedy.

Re:Writing Python like it's Rust by mumblemumble on Sunday May 21, 2023 @04:10PM (#36024283)
I think it's probably no mere coincidence that the article recommends pyright, not mypy.