django-countries icon indicating copy to clipboard operation
django-countries copied to clipboard

Multiple issues with type stubs

Open interDist opened this issue 1 month ago • 0 comments

After upgrading to version 8.1.1, a lot of our code got colored red whilst before type checking was passing. I traced this to various annotations in the fields.pyi file. For example:

CountryField does not define a blank_label attribute, and code that references it gets flagged as “Cannot access attribute "blank_label" for class "CountryField" – Attribute "blank_label" is unknown”.

This line of our code has two problems:

    ', '.join(c.name for c in self.countries)

If self.countries is left as MultipleCountriesDescriptor, the iterator produces unknown values and there is no type checking on c.code and c.name (or, c.qwerty for that matter). This is because def __iter__(self) -> Any: ... while it should’ve been defined with return value of Iterator[Country]. If I force self.countries to be list[Country], the line is flagged as “Argument of type "Generator[str | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[str]" in function "join"”. This is because def name(self) -> str | None: ... but looking at the actual code of Country.name, the property value can never be None, even if c.code is None (related also to #481).

Finally, all country field definitions in our models are now flagged:

    country = CountryField(_("country"))

“No overloads for "__init__" match the provided arguments – Argument types: (StrPromise)”. I believe this is because you defined all __init__ overloads without args. Django’s Field accepts multiple parameters, and they are all defined as args, not kwargs.

interDist avatar Nov 28 '25 01:11 interDist