Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several stub for django.db.models.base.Model are missing #204

Closed
AndreyMZ opened this issue Oct 11, 2019 · 4 comments
Closed

Several stub for django.db.models.base.Model are missing #204

AndreyMZ opened this issue Oct 11, 2019 · 4 comments
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)

Comments

@AndreyMZ
Copy link
Contributor

Bug report

What's wrong

Steps to reproduce

The following code produces the following warning.

from django.db import models

class MyModel(models.Model):
	foo = models.CharField(max_length=255)
	bar = models.CharField(max_length=255)
	
	def clean(self):
		raise self.unique_error_message(MyModel, ('foo', 'bar'))

Unresolved attribute reference 'unique_error_message' for class 'MyModel'

Root cause

No django.db.models.base.Model.unique_error_message method in the stub.

class Model(metaclass=ModelBase):
class DoesNotExist(Exception): ...
class MultipleObjectsReturned(Exception): ...
class Meta: ...
_default_manager: Manager[Model]
_meta: Options[Any]
objects: Manager[Any]
pk: Any = ...
def __init__(self: _Self, *args, **kwargs) -> None: ...
def delete(self, using: Any = ..., keep_parents: bool = ...) -> Tuple[int, Dict[str, int]]: ...
def full_clean(self, exclude: Optional[Collection[str]] = ..., validate_unique: bool = ...) -> None: ...
def clean(self) -> None: ...
def clean_fields(self, exclude: Optional[Collection[str]] = ...) -> None: ...
def validate_unique(self, exclude: Optional[Collection[str]] = ...) -> None: ...
def save(
self,
force_insert: bool = ...,
force_update: bool = ...,
using: Optional[str] = ...,
update_fields: Optional[Union[Sequence[str], str]] = ...,
) -> None: ...
def save_base(
self,
raw: bool = ...,
force_insert: bool = ...,
force_update: bool = ...,
using: Optional[str] = ...,
update_fields: Optional[Union[Sequence[str], str]] = ...,
): ...
def refresh_from_db(self: _Self, using: Optional[str] = ..., fields: Optional[List[str]] = ...) -> _Self: ...
def get_deferred_fields(self) -> Set[str]: ...
@classmethod
def check(cls, **kwargs: Any) -> List[CheckMessage]: ...
def __getstate__(self) -> dict: ...

How is that should be

django.db.models.base.Model.unique_error_message and all other methods of django.db.models.base.Model class must be in the stub:

from typing import Callable, Type
from django.core.exceptions import ValidationError

# <...>

class Model(metaclass=ModelBase):
	# <...>
	def unique_error_message(self, model_class: Type[_Self], unique_check: Collection[Union[Callable, str]]) -> ValidationError: ... 
	# <...>

System information

  • OS: Windows 10
  • python version: 3.6.8
  • django version: 2.2.6
  • mypy version: -
  • PyCharm version: 2019.2.3 (Community Edition)
  • django-stubs version: 1.2.0
@AndreyMZ AndreyMZ added the bug Something isn't working label Oct 11, 2019
@sobolevn
Copy link
Member

@AndreyMZ thanks a lot! Can you please send a PR with the fix?

@AndreyMZ
Copy link
Contributor Author

I have sent a PR with the stub for django.db.models.base.Model.unique_error_message.

But I cannot add stubs for other methods (listed below), because I do not know correct type hints.
Maybe for a start it's worth adding stubs with Any type hint, isn't it?

Methods of django.db.models.base.Model with no stub:

  • __init__
  • from_db
  • __repr__
  • __str__
  • __eq__
  • __hash__
  • __reduce__
  • __getstate__
  • __setstate__
  • _get_pk_val
  • _set_pk_val
  • get_deferred_fields
  • refresh_from_db
  • serializable_value
  • save
  • save_base
  • _save_parents
  • _save_table
  • _do_update
  • _do_insert
  • delete
  • _get_FIELD_display
  • _get_next_or_previous_by_FIELD
  • _get_next_or_previous_in_order
  • prepare_database_save
  • clean
  • validate_unique
  • _get_unique_checks
  • _perform_unique_checks
  • _perform_date_checks
  • date_error_message
  • unique_error_message
  • full_clean
  • clean_fields
  • check
  • _check_swappable
  • _check_model
  • _check_managers
  • _check_fields
  • _check_m2m_through_same_relationship
  • _check_id_field
  • _check_field_name_clashes
  • _check_column_name_clashes
  • _check_model_name_db_lookup_clashes
  • _check_property_name_related_field_accessor_clashes
  • _check_single_primary_key
  • _check_index_together
  • _check_unique_together
  • _check_indexes
  • _check_local_fields
  • _check_ordering
  • _check_long_column_names
  • _check_constraints

AndreyMZ pushed a commit to AndreyMZ/django-stubs that referenced this issue Oct 11, 2019
@sobolevn
Copy link
Member

sobolevn commented Oct 11, 2019

I will rename this issue and merge the PR. Later we will return to this. Ok?

@sobolevn sobolevn changed the title Stub for django.db.models.base.Model.unique_error_message is missing Several stub for django.db.models.base.Model are missing Oct 11, 2019
sobolevn pushed a commit that referenced this issue Oct 11, 2019
…205)

* Fix `django.contrib.admin.options.BaseModelAdmin.has_add_permission` (#203).

* Add `django.db.models.base.Model.unique_error_message` (#204).
@adamchainz
Copy link
Contributor

Most of the remaining attributes are private (start with an underscore). We only add private attributes when there’s a known real-world use case: https://github.com/typeddjango/django-stubs/blob/master/CONTRIBUTING.md#private-attributes . Many probably never need overriding, such as the _check* methods.

The non-private attributes in the list seem to have been added: from_db, serializable_value, prepare_database_save, date_error_message.

So I’ll close this issue now - other attributes can be raised in individual issues or straight up PRs.

@intgr intgr added the stubs Issues in stubs files (.pyi) label Dec 8, 2023
voidus pushed a commit to voidus/django-stubs that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)
Development

No branches or pull requests

4 participants