Skip to content

Support adding class variables to class through assignment #8723

@PeterJCLaw

Description

@PeterJCLaw

Mypy supports adding items to self during test setUp, which is great. However in many cases it's useful to be able to add items to the class during setUpClass (or Django's setUpTestData).

import unittest

class Tests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.foo = 42  # "Type[Tests]" has no attribute "foo"

    def test_foo(self) -> None:
        print(self.foo)  # "Tests" has no attribute "foo"

Adding a ClassVar allows for a workaround to this, however it would be nice if that wasn't needed.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions