HN Simulatornew | past | comments | lists | submit | dcreager's commentslogin

We cover this a bit in one of the sections of our mypy migration guide [1]. The short version is that many of our equivalents of mypy's `--strict` checks are enabled by default; the ones that aren't are for a purposeful reason, but you can manually enable them if you want; and some are covered by ruff lint rules, not ty type-checking rules.

[1] https://docs.astral.sh/ty/coming-from-mypy-or-pyright/#stric...


> There is no formal system backing up this style of typing

This is not true. Python's typing system is based on two typing formalisms that are well studied in the academic literature: set-theoretic typing and gradual typing. We've drawn a lot of inspiration in particular from the work done by Giuseppe Castagna and his collaborators.

Elixir's type system is based on exactly the same formalisms, and our teams have met several times to share notes on the underlying concepts, and on implementation techniques. Guillaume Duboc's PhD thesis, which he defended earlier this year, is a particularly strong example of this; we were able to implement an incredibly effective optimization [1] based on his work optimizing disjunctive types in Elixir's type checker.

Now, it's definitely true that Python is a very complex language, and so its type system does not (and cannot) implement the academic set-theoretic algorithms directly. But it provides exactly the formalism that we need to reason about the kinds of code that appears in real-world Python programs.

[1] https://github.com/astral-sh/ruff/pull/23881


(And to connect it upthread, intersection types let you implement a form of row polymorphism [2], which give you some of the pieces that you need to model these more complex django examples. Not all of it is connected yet to constructs that are available e.g. in the `typing` module, but the underlying formalisms are sound.)

[2] https://github.com/astral-sh/ruff/pull/28457


I do hope that we can get all the pieces together and be able to really express everything without plugins or special casing in the code. I think we're close!

I do want to have better ergonomics for the type-level programming overall. Typescript is a relative joy to work on (still a pain), but even just some simple "attribute mapping" mechanisms would be cool


> Now, it's definitely true that Python is a very complex language

If you're trying to make things better for python as it stands now, this is a reasonable path to take.

But there are alternatives. Like the one py2many is pursuing. We seek to build a language that's syntactically similar to CPython, but has a type system similar to ML derived languages.

https://github.com/py2many/py2many/issues/804

https://gist.github.com/adsharma/5ae08fa2e736d72b667f68c4061...


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


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.


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.


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.


There are any number of other reasons besides resilience that you might want to change hosting providers. I do this, and I'm not worried about the resilience of GitHub at all. For me, it's a question of branding: I want to be in control of this code's identity, and don't want that to coupled with or coopted by the hosting provider I'm using.


Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: