Type hinting for Django Model subclass

2024/9/27 15:23:34

I have helper function for Django views that looks like this (code below). It returns None or single object that matches given query (e.g. pk=1).

from typing import Type, Optionalfrom django.db.models import Modeldef get_or_none(cls: Type[Model], **kwargs) -> Optinal[Model]:try:return cls.objects.get(**kwargs)except cls.DoesNotExist:return None

Supose I have created my own model (e.g. Car) with its own fields (e.g. brand, model). When I asign results of get_or_none function to a varibale, and then retriveing instance fields, I get annoying warning in PyCharm of unresolved reference.

car1 = get_or_none(Car, pk=1)if car1 is not None:print(car1.brand) # <- Unresolved attribute reference 'brand' for class 'Model'

What's the propper type hinting to get rid of this warning and get code completion for variable)?

Answer

Found this answer to almost simmilar question. The solution is to use TypeVar from typing

from typing import TypeVar, Type, Optionalfrom django.db.models import ModelT = TypeVar('T', bound=Model)def get_or_none(cls: Type[T], **kwargs) -> Optional[T]:... # same code

And everything works fine: no warnings and code completion

https://en.xdnf.cn/q/71442.html

Related Q&A

How to diff the two files using Python Generator

I have one file of 100GB having 1 to 1000000000000 separated by new line. In this some lines are missing like 5, 11, 19919 etc. My Ram size is 8GB.How to find the missing elements.My idea take another …

How to resolve: attempted relative import with no known parent package [duplicate]

This question already has answers here:Attempted relative import with no known parent package [duplicate](4 answers)Closed 2 years ago.I have a bare bones project structure with mostly empty python fil…

How to create a figure of subplots of grouped bar charts in python

I want to combine multiple grouped bar charts into one figure, as the image below shows. grouped bar charts in a single figure import matplotlib import matplotlib.pyplot as plt import numpy as nplabels…

Python Pillow: Make image progressive before sending to 3rd party server

I have an image that I am uploading using Django Forms, and its available in the variable as InMemoryFile What I want to do is to make it progressive.Code to make an image a progressiveimg = Image.open…

Python - Should one start a new project directly in Python 3.x?

What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)UPDATE: according to the answers belo…

Produce random wavefunction

I need to produce a random curve in matplotlib.My x values are from say 1 to 1000 for example. I dont want to generate scattered random y values, I need a smooth curve. Like some kind of very distorted…

How to reference groupby index when using apply, transform, agg - Python Pandas?

To be concrete, say we have two DataFrames:df1:date A 0 12/1/14 3 1 12/1/14 1 2 12/3/14 2 3 12/3/14 3 4 12/3/14 4 5 12/6/14 5df2:B 12/1/14 10 12/2/14 20 12/3/14 10 12/4/14 30 12/5/14 10 …

Google AppEngine Endpoints Error: Fetching service config failed (status code 404)

I am implementing the steps in the Quickstart.I did notice another question on this. I double checked that env_variables section in app.yaml has the right values for ENDPOINTS_SERVICE_NAME and ENDPOIN…

How to unload a .NET assembly reference in IronPython

After loading a reference to an assembly with something like:import clr clr.AddRferenceToFileAndPath(rC:\foo.dll)How can I unload the assembly again?Why would anyone ever want to do this? Because Im …

Bad key axes.prop_cycle Error while using an mplstyle in matplotlib (Python)

I am getting the following error when I try to use an external style sheet loaded locally. Bad key "axes.prop_cycle" on line 270 in idt.mplstyle. You probably need to get an updated matplotli…