Hacker News new | ask | show | jobs
by true_religion 2 days ago
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.

1 comments

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.

That’s the bare minimum because it’s the set of features where partial type checking is worse than no type checking, not because I think it represents the average Django application.

For example, if a type checker can accurately analyze objects.filter(x=Foo()) for a foreign keyed Foo, and require that “x” have that type, that seems useful on its own … but if the type checker then does not check the types of x__in=[Foo(), Bar()], that’s worse than useless: folks will get used to type protection in the first case and expect it in the latter, and be disappointed in the form of production bug rates going up.

That’s why I picked the features I did for “Django bare minimum” type checking. If you typecheck that stuff any less than really thoroughly, correctly, and covnveniently, the ROI of the type checker may become negative.

That’s why, for many projects, I find “type annotate and check business logic, leave Django largely un-checked (consider annotations to be notes for humans) and prioritize test coverage there instead” to be the right move.