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

Cannot type objects custom manager as ClassVar[ManagerClass] #1909

Closed
federicobond opened this issue Jan 17, 2024 · 3 comments
Closed

Cannot type objects custom manager as ClassVar[ManagerClass] #1909

federicobond opened this issue Jan 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@federicobond
Copy link
Contributor

Bug report

What's wrong

I cannot explicitly type attribute as custom model manager when assigning to the model objects attribute.

from typing import ClassVar
from django.db import models

class A(models.Model):
    objects: ClassVar[models.Manager] = models.QuerySet.as_manager()

This returns:

models.py:7: error: Cannot override instance variable (previously declared on base class "Model") with class variable  [misc]
        objects: ClassVar[models.Manager] = models.QuerySet.as_manager()
        ^~~~~~~

How is that should be

It should type correctly.

System information

  • OS: Linux
  • python version: 3.11
  • django version: 5.0.1
  • mypy version: 1.8.0
  • django-stubs version: latest
  • django-stubs-ext version: latest
@federicobond federicobond added the bug Something isn't working label Jan 17, 2024
@federicobond
Copy link
Contributor Author

Removing the type or typing as Any causes mypy to crash with:

Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/usr/local/lib/python3.11/site-packages/mypy/main.py", line 100, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/main.py", line 182, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 191, in build
    result = _build(
             ^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 265, in _build
    graph = dispatch(sources, manager, stdout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 2943, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 3341, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 3468, in process_stale_scc
    graph[id].write_cache()
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 2504, in write_cache
    new_interface_hash, self.meta = write_cache(
                                    ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 1566, in write_cache
    data_str = json_dumps(data, manager.options.debug_cache)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mypy/build.py", line 1511, in json_dumps
    return json.dumps(obj, sort_keys=True, separators=(",", ":"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AnyType is not JSON serializable

@federicobond
Copy link
Contributor Author

Interesting... looks like it does type correctly when I remove the explicit type and call it with --cache-dir=/dev/null, so it appears it could be an issue with TypeInfo metadata, as described here.

@flaeppe
Copy link
Member

flaeppe commented Mar 22, 2024

I've tried to but can't reproduce any error or problems with the provided snippet. I'm happy to reopen if you can provide a repro case.

@flaeppe flaeppe closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 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
Development

No branches or pull requests

2 participants