[Python] How do I get VSCode Intellisense to work with my python class factory method?

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python class factory method that I use to create a commonly implemented derived class of django.tests.TestCase and django.tests.TransactionTestCase:

    from typing import Type

    from django.test import TestCase, TransactionTestCase

    def test_case_class_factory(base_class) -> Type[TestCase]:

    class MyDerivedTestCase(base_class):
    ...

    return MyDerivedTestCase

    TracebaseTestCase: TestCase = test_case_class_factory(TestCase)
    TracebaseTransactionTestCase: TransactionTestCase = test_case_class_factory(TransactionTestCase)


    These classes work well. I've used them for years now, but when I inherit from either of those classes, VSCode's Intellisense and syntax highlighting has never worked, e.g.:

    from DataRepo.tests.tracebase_test_case import TracebaseTestCase

    class MyTests(TracebaseTestCase):

    def test_multiply(self):
    self.assertEqual(6.2, multiply(2.0, 3.1))


    E.g. assertEqual is white:

    [​IMG]

    I've tried a few things today in terms of type hinting, but I can't seem to figure out how to make it work. How can I make inheriting from these factory-created classes be compatible with VSCode's intellisense and syntax highlighting?

    Continue reading...

Compartilhe esta Página