mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-09 13:35:01 +08:00
Add "one_to_many", "one_to_one", "many_to_many," and "many_to_one" attributes to Field base class (#449)
* Add "_to_many" and "_to_one" bool attributes to Field base class. * Remove unused import from test case.
This commit is contained in:
@@ -56,6 +56,10 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
|
||||
remote_field: Field
|
||||
is_relation: bool
|
||||
related_model: Optional[Type[Model]]
|
||||
one_to_many: Optional[bool] = ...
|
||||
one_to_one: Optional[bool] = ...
|
||||
many_to_many: Optional[bool] = ...
|
||||
many_to_one: Optional[bool] = ...
|
||||
max_length: int
|
||||
model: Type[Model]
|
||||
name: str
|
||||
|
||||
26
test-data/typecheck/db/models/test_init.yml
Normal file
26
test-data/typecheck/db/models/test_init.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
- case: field_to_many_and_to_one_attrs_bool_or_none_in_field_base_class
|
||||
main: |
|
||||
from django.db.models import Field
|
||||
|
||||
field: Field
|
||||
my_bool: bool
|
||||
|
||||
my_bool = field.one_to_many
|
||||
my_bool = field.one_to_one
|
||||
my_bool = field.many_to_many
|
||||
my_bool = field.many_to_one
|
||||
|
||||
# Narrowing the types should give us bool
|
||||
assert field.one_to_many is not None
|
||||
my_bool = field.one_to_many
|
||||
assert field.one_to_one is not None
|
||||
my_bool = field.one_to_one
|
||||
assert field.many_to_many is not None
|
||||
my_bool = field.many_to_many
|
||||
assert field.many_to_one is not None
|
||||
my_bool = field.many_to_one
|
||||
out: |
|
||||
main:6: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
|
||||
main:7: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
|
||||
main:8: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
|
||||
main:9: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
|
||||
Reference in New Issue
Block a user