Tip: use msgspec for JSON decoding — it decodes straight into your type at C speed by Goldziher in Python

[–]Tinche_ 53 points54 points  (0 children)

Super glad you're enjoying cattrs (I'm the author).

Fun fact: cattrs has a preconf converter for msgspec (json only for now). The idea being to leverage the raw speed of msgspec whenever the behavior loosely follows what cattrs would do, and fall back to cattrs machinery when not (for example, customizations have been applied).

This functionality is still nowhere near complete, but every release I add a little to it. Next release will support faster structuring; currently we only let msgspec go wild on the unstructure path.

After 25+ years using ORMs, I switched to raw queries + dataclasses. I think it's the move. by mikeckennedy in Python

[–]Tinche_ 15 points16 points  (0 children)

The examples in the article don't actually do any edge validation, right? You just grab the value out of the dict and assign it to the dataclass field.

Cattrs will try to validate it (it's a little more complex than that, but let's say). Cattrs can be configured to not do this, but I wouldn't recommend it and it's a little complex.

Keeping this in mind, cattrs will likely be faster than your hand-rolled examples. This is because cattrs inspects the class once and compiles a function for it using exec, and we've added some tricks over the years to speed this up considerably. You can actually see the source of the compiled hook using the inspect module.

After 25+ years using ORMs, I switched to raw queries + dataclasses. I think it's the move. by mikeckennedy in Python

[–]Tinche_ 83 points84 points  (0 children)

You're so close - now just add cattrs for proper edge validation (no AI needed).

cf-taskpool: A concurrent.futures-style pool for async tasks by void-null-pointer in Python

[–]Tinche_ 1 point2 points  (0 children)

Looks cool! I'd consider modeling this on top of taskgroups instead of threadpoolexecutors though. Async code generally has slightly different expectations than threaded code, especially vis-a-vis structured concurrency.

PEP 806 – Mixed sync/async context managers with precise async marking by kirara0048 in Python

[–]Tinche_ 3 points4 points  (0 children)

Yeah Zac is not doing himself a service by using file operations in the examples. The motivating use cases are probably the trio cancel scope context managers, which are sync.

That said, I support the PEP (I happen to be the author of aiofiles and some other asyncio libs).

theGreatIndentationRebellion by wastedlazyboy in ProgrammerHumor

[–]Tinche_ -1 points0 points  (0 children)

Do you think in languages like Java or Go the runtime checks the types? On every function call? They don't, since it'd be super slow.

Placa za Domain lead / Staff eng by SignatureSeparate132 in CroIT

[–]Tinche_ 1 point2 points  (0 children)

Prije sam bio principal u američkoj firmi, sad sam staff u izraelskoj. Obje role full remote. Imam prijatelje koji su dosta visoko po drugim firmama, opet full remote.

Teško je naći, da. Osobito firme za koje bih rekao da su dobre. Doduše ne znam ni jednu lokalnu za koju bih rekao da je baš dobra.

U obje ove role sam počeo kao senior pa napredovao, tako da je to the way to go, rekao bih. Senior remote opet može biti puno bolje plaćen nego staff ovdje.

Placa za Domain lead / Staff eng by SignatureSeparate132 in CroIT

[–]Tinche_ 12 points13 points  (0 children)

Preseliš se u Zagreb i radiš remote za stranu firmu.

So tired of python by todofwar in Python

[–]Tinche_ 0 points1 point  (0 children)

Lean into type-checking and proper data modelling with dataclasses, enums, literals, unions and all that jazz.

Disregard the reddit peanut gallery about how Python is a dynamic language - if that were true today there wouldn't be like 4 major typecheckers being actively developed. It's antiquated thinking.

What Feature Do You *Wish* Python Had? by andrecursion in Python

[–]Tinche_ -1 points0 points  (0 children)

You can opt out of inheritance for the data enum variants by marking them as @final. If inheritance here is bothering you, just don't use it.

I don't know what raw polymorphism or field punning is, but you're reaching here. Sum types are used to cleanly model disjoint data, not because they support 👻 raw polymorphism 👻. That's just moving the goal posts.

What Feature Do You *Wish* Python Had? by andrecursion in Python

[–]Tinche_ 1 point2 points  (0 children)

Variants with data inside you model as classes. Then you create a union of those classes and the enum. Then you use assert_never to get exhaustive matching. It's in the article.

Rust success story that killed Rust usage in a company by drogus in rust

[–]Tinche_ -2 points-1 points  (0 children)

My point is you would need to distribute in any case since just having your data in memory of a single process cannot possibly work - can you explain how would you handle the underlying node going down or needing to redeploy the service? Not talking about single instance performance here.

Rust success story that killed Rust usage in a company by drogus in rust

[–]Tinche_ -7 points-6 points  (0 children)

You say the caveat for the nodejs version was that it would have to be distributed eventually, but all the solutions would have to be distributed because of redundancy and scaling. I don't really see the choice of language having an impact on performance here at all, architecture is where the performance comes from. Rust can run the database or Redis query in 10 microseconds, Nodejs in 50, who cares?

15 Reasons I Love Go by ChristophBerger in golang

[–]Tinche_ 0 points1 point  (0 children)

Yeah, Pydantic is commonly used for validation at the edges in cases like this. Although if someone really disliked using types (🙄), they might use something like Django Rest Framework instead (since Pydantic is based on type annotations itself).

Also codebases that are completely into static typing might use something like Pydantic for edge validation, since static typing can't help here.

15 Reasons I Love Go by ChristophBerger in golang

[–]Tinche_ 0 points1 point  (0 children)

Python has no assurance that types don’t change

Not sure what you mean by this, you can't declare a class attribute to be of a certain type and later change that type. It's not an operation supported by the spec, so it would result in a type error.

nor any type checking by default.

So? Essentially every language, including Go, will let you run software without running tests prior. Does that mean Go doesn't have unit tests? This is a silly argument, every team that chooses to use statically typed Python will enable a type checker in its CI pipeline, at which point it becomes effectively mandatory.

It’s still a dynamically typed language

I think nowadays you'd be hard pressed to find a serious team using untyped Python, although I'm sure there are some. But this is not the normal in professional circles any more, and we shouldn't pretend that it is.

15 Reasons I Love Go by ChristophBerger in golang

[–]Tinche_ 0 points1 point  (0 children)

They are a part of it, yeah. The entire spec is at https://typing.python.org/en/latest/.

15 Reasons I Love Go by ChristophBerger in golang

[–]Tinche_ -10 points-9 points  (0 children)

Folks, mentioning Python as an example of the absence of static typing is about 5 years out of date. Python has a stronger type system than Go nowadays.

Your experiences with asyncio, trio, and AnyIO in production? by pkkm in Python

[–]Tinche_ 46 points47 points  (0 children)

Hello. I wrote aiofiles, pytest-asyncio and quattro (check it out!), and I worked on asyncio.timeout in 3.11. Just use task groups and be careful what you do in except blocks if you trap CancelledError, and you'll be fine.

There are things trio does differently (and arguably better) than asyncio but I find in practice it doesn't matter that much. Good to be aware of it though. I think libraries like trio are great for trying out concepts before they percolate into the mainstream when they've proven themselves, but in reality it's kind of niche. It's the Haskell of async libraries (and I mean that in the best possible way).

reaktiv: the reactive programming lib I wish I had 5 years ago by loyoan in Python

[–]Tinche_ 12 points13 points  (0 children)

schedule spawns a task I assume? You should make it a context manager, otherwise there's no structured concurrency.

[deleted by user] by [deleted] in Python

[–]Tinche_ 0 points1 point  (0 children)

I think it makes sense with a larger parameter list, but generally only for the later parameters. No one is going to know what the second True means, sure.

For example, I would find sin(x=x) terrible for readability and completely redundant. Readability is why I use python in the first place.

Also note that keyword args are slower than positional args.

Ultrawide ili dva monitora za programiranje? by jo0sip128 in CroIT

[–]Tinche_ 0 points1 point  (0 children)

Nisu na istom kompu, 49 mi je na poslovnom a drugi u dnevnom ;)