Skip to content

Commit 8787933

Browse files
committed
Fix multiple type stub issues in fields.pyi (#483)
- Fix return types: Country.name, alpha3, ioc_code now return str (not str | None) - Fix Country.numeric return type to int | None (was str | None) - Fix MultipleCountriesDescriptor.__iter__ to return Iterator[Country] (was Any) - Add missing blank_label attribute and other instance attributes to CountryField - Accept positional verbose_name in CountryField.__init__ for Django compatibility - Add missing __contains__ and __add__ methods to MultipleCountriesDescriptor
1 parent e40f591 commit 8787933

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

changes/483.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix multiple type stub issues in `fields.pyi`: correct return types for `Country.name`, `Country.alpha3`, `Country.ioc_code` (now `str` instead of `str | None`), `Country.numeric` (now `int | None` instead of `str | None`), and `MultipleCountriesDescriptor.__iter__` (now `Iterator[Country]` instead of `Any`). Also add missing `blank_label` attribute and accept positional `verbose_name` argument in `CountryField.__init__` overloads.

django_countries/fields.pyi

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ This stub file provides enhanced type hints for better IDE support.
55
The actual implementation is in fields.py.
66
"""
77

8-
from typing import Any, Iterable, Literal, overload
8+
from typing import Any, Iterable, Iterator, Literal, overload
99

1010
from django.db import models
11+
from django.utils.functional import _StrPromise
1112
from typing_extensions import Self, TypeAlias
1213

1314
from django_countries import Countries
@@ -36,13 +37,15 @@ class Country:
3637
def __eq__(self, other: object) -> bool: ...
3738
def __hash__(self) -> int: ...
3839
@property
39-
def name(self) -> str | None: ...
40+
def name(self) -> str: ...
4041
@property
41-
def alpha3(self) -> str | None: ...
42+
def alpha3(self) -> str: ...
4243
@property
43-
def numeric(self) -> str | None: ...
44+
def numeric(self) -> int | None: ...
4445
@property
45-
def ioc_code(self) -> str | None: ...
46+
def numeric_padded(self) -> str | None: ...
47+
@property
48+
def ioc_code(self) -> str: ...
4649
@property
4750
def unicode_flag(self) -> str: ...
4851
@property
@@ -54,8 +57,12 @@ class MultipleCountriesDescriptor:
5457
def __init__(self, countries_iter: Iterable[Country]) -> None: ...
5558
def __str__(self) -> str: ...
5659
def __repr__(self) -> str: ...
57-
def __iter__(self) -> Any: ...
60+
def __iter__(self) -> Iterator[Country]: ...
5861
def __getitem__(self, index: int) -> Country: ...
62+
def __contains__(self, item: object) -> bool: ...
63+
def __add__(
64+
self, other: Iterable[Country | str]
65+
) -> MultipleCountriesDescriptor: ...
5966
def __len__(self) -> int: ...
6067
def __bool__(self) -> bool: ...
6168
def __eq__(self, other: object) -> bool: ...
@@ -104,11 +111,17 @@ class CountryField(models.CharField):
104111

105112
descriptor_class: type[CountryDescriptor]
106113
multiple: bool
114+
blank_label: str | None
115+
countries: Countries
116+
countries_flag_url: str | None
117+
countries_str_attr: str
107118

108119
# Overload for single, non-nullable (most common)
109120
@overload
110121
def __init__(
111122
self,
123+
verbose_name: str | _StrPromise | None = None,
124+
name: str | None = None,
112125
*,
113126
multiple: Literal[False] = False,
114127
null: Literal[False] = False,
@@ -124,6 +137,8 @@ class CountryField(models.CharField):
124137
@overload
125138
def __init__(
126139
self,
140+
verbose_name: str | _StrPromise | None = None,
141+
name: str | None = None,
127142
*,
128143
multiple: Literal[False] = False,
129144
null: Literal[True],
@@ -139,6 +154,8 @@ class CountryField(models.CharField):
139154
@overload
140155
def __init__(
141156
self,
157+
verbose_name: str | _StrPromise | None = None,
158+
name: str | None = None,
142159
*,
143160
multiple: Literal[True],
144161
null: Literal[False] = False,
@@ -148,13 +165,16 @@ class CountryField(models.CharField):
148165
countries: type[Countries] | None = None,
149166
countries_flag_url: str | None = None,
150167
countries_str_attr: str = "code",
168+
blank_label: str | None = None,
151169
**kwargs: Any,
152170
) -> None: ...
153171

154-
# Overload for multiple, nullable (new in PR #453)
172+
# Overload for multiple, nullable
155173
@overload
156174
def __init__(
157175
self,
176+
verbose_name: str | _StrPromise | None = None,
177+
name: str | None = None,
158178
*,
159179
multiple: Literal[True],
160180
null: Literal[True],
@@ -164,5 +184,6 @@ class CountryField(models.CharField):
164184
countries: type[Countries] | None = None,
165185
countries_flag_url: str | None = None,
166186
countries_str_attr: str = "code",
187+
blank_label: str | None = None,
167188
**kwargs: Any,
168189
) -> None: ...

0 commit comments

Comments
 (0)