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

AssertionError when using models.Manager #373

Closed
dmwyatt opened this issue May 26, 2020 · 14 comments · Fixed by #424
Closed

AssertionError when using models.Manager #373

dmwyatt opened this issue May 26, 2020 · 14 comments · Fixed by #424
Assignees
Labels
bug Something isn't working

Comments

@dmwyatt
Copy link

dmwyatt commented May 26, 2020

Bug report

What's wrong

I can't seem to create a minimal example demonstrating this, but I'm getting this on one of my projects.

If I change a custom queryset to become a custom manager by changing what it inherits from and changing objects = CustomQuerySet.as_manager() to objects = CustomManager() I get the following.

Traceback (most recent call last):
  File "c:\users\dusti\.pyenv\pyenv-win\versions\3.8.0-amd64\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
    exec(code, run_globals)
  File "mypy\semanal.py", line 4679, in accept
  File "mypy\nodes.py", line 939, in accept
  File "mypy\semanal.py", line 1001, in visit_class_def
  File "mypy\semanal.py", line 1078, in analyze_class
  File "mypy\semanal.py", line 1087, in analyze_class_body_common
  File "mypy\semanal.py", line 1147, in apply_class_plugin_hooks
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\main.py", line 37, in transform_model_class
    process_model_class(ctx, django_context)
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\transformers\models.py", line 357, in process_model_class
    initializer_cls(ctx, django_context).run()
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\transformers\models.py", line 71, in run
    self.run_with_model_cls(model_cls)
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\transformers\models.py", line 230, in run_with_model_cls
    custom_manager_type = self.create_new_model_parametrized_manager(custom_model_manager_name,
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\transformers\models.py", line 183, in create_new_model_parametrized_manager
    helpers.copy_method_to_another_class(new_cls_def_context,
  File "c:\users\dusti\appdata\local\pypoetry\cache\virtualenvs\vehicles-caet0sxz-py3.8\lib\site-packages\mypy_django_plugin\lib\helpers.py", line 346, in copy_method_to_another_class
    assert bound_return_type is not None
AssertionError:
  • OS: Win10
  • python version: 3.8
  • django version: 3.0.6
  • mypy version: .770
  • django-stubs version: 1.5.0
@dmwyatt dmwyatt added the bug Something isn't working label May 26, 2020
@davepeck
Copy link

davepeck commented Jun 5, 2020

I'm running into this bug too, under the same circumstances: changed a QuerySet to a Manager

@adithyabsk
Copy link

adithyabsk commented Jun 16, 2020

Yep, I'm getting the same issue here.

Python 3.6.8
Django 2.2.3
mypy 0.770
django-stubs 1.5.0

@kszmigiel
Copy link
Member

@dmwyatt @davepeck @adithyabsk Could some of you guys support me with an example code causing the bug occur, preferably together with Mypy config file and the command you're using? I'm trying to reproduce this bug but with no success so far.

@sobolevn
Copy link
Member

Or even with a reproduction repo 😉

danifus added a commit to danifus/djstubs-reproduce that referenced this issue Jul 11, 2020
@danifus
Copy link
Contributor

danifus commented Jul 11, 2020

I've experienced a similar traceback. I've created a reproduction repo: https://github.com/danifus/djstubs-reproduce

Steps:

  • cd to the directory containing mypy.ini
  • run mypy . --tb

Gives me:

./test_app/models.py:10: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.770
Traceback (most recent call last):
  File "mypy/semanal.py", line 4679, in accept
  File "mypy/nodes.py", line 939, in accept
  File "mypy/semanal.py", line 1001, in visit_class_def
  File "mypy/semanal.py", line 1078, in analyze_class
  File "mypy/semanal.py", line 1087, in analyze_class_body_common
  File "mypy/semanal.py", line 1147, in apply_class_plugin_hooks
  File "<venv_dir>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/main.py", line 37, in transform_model_class
    process_model_class(ctx, django_context)
  File "<venv_dir>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 357, in process_model_class
    initializer_cls(ctx, django_context).run()
  File "<venv_dir>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 71, in run
    self.run_with_model_cls(model_cls)
  File "<venv_dir>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 231, in run_with_model_cls
    base_manager_info=manager_info)
  File "<venv_dir>mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 186, in create_new_model_parametrized_manager
    method_node=sym.node)
  File "<venv_dir>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/lib/helpers.py", line 355, in copy_method_to_another_class
    assert bound_arg_type is not None
AssertionError:
./test_app/models.py:10: : note: use --pdb to drop into pdb

Let me know if you need anything else to help this along.

@danifus
Copy link
Contributor

danifus commented Jul 11, 2020

Not sure if it is the minimal reproduction repo but it resembles the structure that triggered this for me

@kszmigiel
Copy link
Member

@danifus Thanks, getting to work

@kszmigiel
Copy link
Member

@davepeck @danifus @adithyabsk @dmwyatt

Changing

class ManagerA(models.Manager):

    def do_something(self, other_obj: "ModelB"):
        return None


class ModelA(models.Model):
    title = models.TextField()

    objects = ManagerA()


class ModelB(models.Model):
    movie = models.TextField()

to

class ModelB(models.Model):
    movie = models.TextField()

class ManagerA(models.Manager):

    def do_something(self, other_obj: "ModelB"):
        return None


class ModelA(models.Model):
    title = models.TextField()

    objects = ManagerA()

helped 😄 You have to specify a class before using it as type hint. Let me know if it works for you.

@sobolevn
Copy link
Member

@kszmigiel can this be solved using .defer()? We need to be sure that the order is not important.
Otherwise, we would have a lot of bug reports like this one.

@danifus
Copy link
Contributor

danifus commented Jul 12, 2020

@kszmigiel That's the workaround I'm currently using but as it is a forward reference to ModelB in def do_something(self, other_obj: "ModelB"):, ModelB should be able to be defined after it is used as a mypy type.

@danifus
Copy link
Contributor

danifus commented Jul 12, 2020

Looks to be a little related to: #344

They mention that mypy==0.750 and django-stubs==1.3.2 worked for their deferred types. The same appears to be true here, this bug is not triggered in the reproduction repo with those versions.

@danifus
Copy link
Contributor

danifus commented Jul 12, 2020

Git bisect points to 1196336 as the commit that introduced this issue.

@sobolevn
Copy link
Member

@danifus thanks a lot for the crime scene investigation! 🚓

My wild guess that python/mypy#7071 can possibly solve this issue.

@danifus
Copy link
Contributor

danifus commented Jul 13, 2020

Thanks guys! Working for me now :D

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

Successfully merging a pull request may close this issue.

6 participants