Hacker News new | ask | show | jobs
by dcreager 1 day ago
Ideally it shouldn't; I don't think anyone would argue otherwise.

But (1) django is older than Python's type system, so it uses some very dynamic patterns that are hard to express using Python types. And (2) it's very popular, which means both that lots of people want their type checker to do Something Useful™ with django code, and that it would be very difficult to retrofit django to use dynamic patterns that _are_ more compatible with the type system. That combination makes it worth our time to add specific custom support for it.

For your own future library, the hope is that the type system has become capable enough that you wouldn't _need_ custom type checker support. Things like `dataclass_transform` [1] were added specifically so that _all_ libraries could express these kinds of things in ways that are visible to the type checkers without custom support.

If you're doing something that still doesn't fit into what the typing system provides, then the Python typing community wants to learn about your use case so that we can see if we can extend the typing system even further to support it.

[1] https://typing.python.org/en/latest/spec/dataclasses.html

2 comments

Django doesn't really do any too crazy. Most use cases would be filled if there were a way to declare:

- Adding field X also adds field x_pk

- Adding field X also changes the signature of function Y to accept parameter x, x__lt, x__gt, etc.

- Adding field X lets you do a.x = 5, even though X is not really an integer

As an aside, I have to give it to the Typescript team for taking Javascript which was fundamentally as metaprogramming happy as Python, and creating a type system that matched it. Having a goal that every popular pattern in Javascript should be type-checked was a great starting choice, because they couldn't simply copy some other languages' static typing methadology and had to work from teh ground up.

There is a lot more than that. Reverse FKs, chained relations (foo__foofk__barfk__in), complexity regarding Django's several understandings of "abstract"/interfaces/protocols (abstract models, form classes that implicitly can or must provide methods with specific names/signatures depending on what fields are on the form, etc.), runtime-generated model classes (how and where to check in or tell the typechecker about those stubs?), lazy objects that change type (gaining and removing/changing behavior) at runtime based on things that are pretty hard for a type checker to notice, like random field access/indexing, and so on.

And that's just the core Django dynamism; the stuff that needs to work really well. Not even getting into stuff that would be nice, like custom managers providing methods to model instances, Q/F type helpers, any sort of typing help with templates (even just "can this method be called with the signature this template wants"), complex model introspection typing, something cleaner than pydantic/manual TypedDicts for form data at the ser/de boundary, and so on.

Even that bare minimum requires some pretty difficult features, type-checker wise: extremely distant dependent types ("if field X exists, method Y should take kwargs X__in and so on" as in your example, as well as "if field X exists, method get_X must be declared on class Y with signature Z", and "if field X exists, then a very distant method in a model manager needs to be documented to return type Y if values_list=True"), very robust support for runtime-created signatures that in a lot of Django systems cannot be generated and checked in (e.g. database-derived models or JSON-backed fields outside of Postgres), extremely complex dependent typing in classes whose generic bounds are determined pretty late/at mixin time (but which may also conceptually be mixins that can only be used in a very few appropriate cases), and more.

Like, just think about model field names in manager method arguments--a tiny fraction of the problem. All the different chained/related kwargs would have to be computed and typed, as well as their disjoint: if I rename field "foo" to "bar", and forget to update some caller that does "m.objects.filter(thing__thing2_fk__foo__in=[1,2])", it'd be great if the type checker could help me catch that. But even if it can't, does that mean that "objects.filter" has all-optional typed kwargs and also takes **kwargs as well, because the type coverage is gradual? As soon as that's allowed, a whole lot of useful typechecking abilities go out the window since misnamed keys can just fall into the wildcard. Solvable, sure, but a major pain in the ass to implement--especially given that Django is a fast-moving target adding/removing/changing functionality all the time.

Again, that's just Django basics. Things that ordinary, bog-standard, not-using-advanced-functionality Django code needs all the time. If the typechecker mishandles one of those cases and makes life harder, people will abandon it. There's a very tricky line to walk here re: when to be strict and when to give up on typechecking entirely, but Django has a lot of places where the latter is necessary.

Your bare minimum is far more complicated than mine.

However my point was: type checkers of today can’t even handle the far more simple desires that I have.

It seems they aren’t committed towards dynamism as a default.

The Python typing community has known about the expressiveness problems for years. It's forward-only, has no HKTs, no type computation, no complement. I can't even do generic parameter list transforms.

All of these are known deficiencies. Everyone who brings them up is told to pound sand, their use cases deemed illegitimate. Might as well be GNOME.

It's fundamentally unfair that Django gets to use aspects of the language other tools can't, and one hard coded dataclass-style transform is inadequate.

So help me God, if I ever do another large scale Python project, then if these issues aren't fixed in typing, I will vendor and modify a type checker in the project itself and teach it my project's patterns directly. I will not twist and distort my design vision so it rhymes with dataclass.

> It's fundamentally unfair that Django gets to use aspects of the language other tools can't

You can just ... not typecheck parts of your code. All of these tools are gradual, since Python's much too dynamic to encompass everything. Falling back to runtime "I know how this works" dynamism isn't a sin, it just requires a little care. Add restrictions (typed wrappers, verbosity/limitations a la dataclass_transform) at the boundaries where people make frequent mistakes, and move on with your life.