Skip to content

Conversation

linw1995
Copy link
Contributor

@linw1995 linw1995 commented Aug 1, 2021

Description

The get_dynamic_class_hook method of the plugin never called if the statement is a chained assignment like Bad1 = Bad2 = declarative_base(); Or if the statement is a chain of call expressions like Base = declarative_base().with_optionalA().

Test Plan

  1. Update existed unit test "mypy/test/testcheck.py::TypeCheckSuite::testDynamicClassPluginNegatives"
[case testDynamicClassPluginNegatives]
# flags: --config-file tmp/mypy.ini
from mod import non_declarative_base

Bad1 = non_declarative_base()

class C1(Bad1): ...  # E: Variable "__main__.Bad1" is not valid as a type \
                     # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases \
                     # E: Invalid base class "Bad1"
[file mod.py]
def non_declarative_base(): ...
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/dyn_class.py

  1. Add two new unit tests
[case testDynamicClassPluginChainCall]
# flags: --config-file tmp/mypy.ini
from mod import declarative_base, Column, Instr

Base = declarative_base().with_optional_xxx()

class Model(Base):
    x: Column[int]

reveal_type(Model().x)  # N: Revealed type is "mod.Instr[builtins.int]"
[file mod.py]
from typing import Generic, TypeVar

class Base:
    def with_optional_xxx(self) -> Base: ...

def declarative_base() -> Base: ...

T = TypeVar('T')

class Column(Generic[T]): ...
class Instr(Generic[T]): ...

[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/dyn_class.py

[case testDynamicClassPluginChainedAssignment]
# flags: --config-file tmp/mypy.ini
from mod import declarative_base

Base1 = Base2 = declarative_base()

class C1(Base1): ...  
class C2(Base2): ...
[file mod.py]
def declarative_base(): ...
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/dyn_class.py

@linw1995
Copy link
Contributor Author

linw1995 commented Aug 1, 2021

https://app.travis-ci.com/github/python/mypy/jobs/528284498

CI job "run test suite with python nightly" failed and produced a lot of error outputs like below

________________________ testDivReverseOperatorPython2 _________________________
[gw1] linux -- Python 3.10.0 /home/travis/build/python/mypy/.tox/py/bin/python
data: /home/travis/build/python/mypy/test-data/unit/check-classes.test:2170:
SystemExit: 1
----------------------------- Captured stderr call -----------------------------
The typed_ast package is not installed.
For Python 2 support, install mypy using `python3 -m pip install "mypy[python2]"`Alternatively, you can install typed_ast with `python3 -m pip install typed-ast`.
______________________________ testNotAsBinaryOp _______________________________
[gw0] linux -- Python 3.10.0 /home/travis/build/python/mypy/.tox/py/bin/python
data: /home/travis/build/python/mypy/test-data/unit/parse.test:934:
/home/travis/build/python/mypy/mypy/test/testparse.py:22: in run_case
    test_parser(testcase)
/home/travis/build/python/mypy/mypy/test/testparse.py:46: in test_parser
    assert_string_arrays_equal(testcase.output, a,
E   AssertionError: Invalid parser output (/home/travis/build/python/mypy/test-data/unit/parse.test, line 934)
----------------------------- Captured stderr call -----------------------------
Expected:
  main:1: error: invalid syntax. Perhaps you forgot a comma? (diff)
Actual:
  main:1: error: invalid syntax                 (diff)
Alignment of first line difference:
  E: main:1: error: invalid syntax. Perhaps you forgot a comma?
  A: main:1: error: invalid syntax

and more

@pranavrajpal
Copy link
Contributor

The python nightly CI has been broken for a while (which is why it's marked as allowed to fail).

The mypyc errors seem to be related to mypyc/mypyc#864. Moving the class definition for CallExprVisitor to module level should fix those CI errors.

@linw1995
Copy link
Contributor Author

linw1995 commented Aug 2, 2021

The mypyc errors seem to be related to mypyc/mypyc#864. Moving the class definition for CallExprVisitor to module level should fix those CI errors.

The latest commit resolves this kind of errors. @pranavrajpal And thanks to you.

@linw1995
Copy link
Contributor Author

linw1995 commented Aug 8, 2021

Friendly ping, can I get a review? : )

@linw1995
Copy link
Contributor Author

Gently ping @pranavrajpal. Can I get a review?

Copy link
Contributor

@pranavrajpal pranavrajpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long to review this.

This mostly looks good to me (see my one comment), but you'll probably have to wait for a review from a maintainer.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and sorry for the slow response! Looks good.

@JukkaL JukkaL merged commit f316cc2 into python:master Nov 12, 2021
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
)

The get_dynamic_class_hook method of the plugin never called if the statement 
is a chained assignment like `Bad1 = Bad2 = declarative_base()`; or if the statement 
is a chain of call expressions like `Base = declarative_base().with_optionalA()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants