#35406: Using Django models in function type annotations, without dependency to
settings.configure()
-------------------------------------+-------------------------------------
     Reporter:  HTErik               |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  models, typing       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by HTErik):

 Hi. Thanks for the reply.
 I'm already aware of django-stubs and we are already using it for long
 time with success.
 The difficulty here is not getting mypy to work.

 This is a problem with how Django relies deeply on global state
 initialization with the `settings.configure()`, and how the order of how
 you `import` models vs `configure` Django have significant differences,
 that are not only confusing and unexpected to the average Python
 developer, but also impossible to untangle even for the experienced.

 Whenever one type-annotates a function to take any Django-model as input,
 that infects the entire code base, so all code that even just imports this
 function must be **declared and imported**  *after*  `settings.configure`
 is called.  Otherwise you can not even *import* the function. (Not being
 able to *call* it once the application is running is totally expected)

 Because of this, using django models in an components that are shared
 across multiple services, that all may not be pure `manage.py`
 applications, is today more or less impossible, if you at the same time
 want to support type hinting your application completely.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35406#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018f77d257d7-79afd397-8e8e-431f-86f4-5c6b366e115e-000000%40eu-central-1.amazonses.com.

Reply via email to