Skip to content

"type[Model]" has no attribute "objects" error #1754

Closed as not planned
Closed as not planned
@jewelvadim

Description

@jewelvadim

Bug report

Mypy error in function

from contextlib import suppress
from typing import Type

from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Model


def get_next_pk(model: Type[Model]) -> int:
    pk = 1
    with suppress(ObjectDoesNotExist):
        pk = model.objects.latest('pk').pk + 1

    return pk

mypy.ini

[mypy]
python_version = 3.11
plugins = mypy_django_plugin.main, mypy_drf_plugin.main
exclude = .git, .idea, .mypy_cache, .ruff_cache, node_modules
cache_dir = ./.cache/mypy
check_untyped_defs = true
disallow_untyped_decorators = true
disallow_untyped_calls = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
no_implicit_optional = true
strict_optional = true
strict_equality = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true

[mypy.plugins.django-stubs]
django_settings_module = 'settings'

What's wrong

After run mypy . I receive next error:

apps/core/functions/get_next_pk.py:11: error: "type[Model]" has no attribute "objects"  [attr-defined]

System information

  • OS: Ubuntu 22.04
  • python version: 3.11
  • django version: 4.2.5
  • mypy version: 1.5.1
  • django-stubs version: 4.2.4
  • django-stubs-ext version: 4.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions