Ah! I did it!
Here's how I got it to work:
from typing import Type, TypeVar
from django.test import TestCase, TransactionTestCase
T = TypeVar("TBTC", TestCase, TransactionTestCase)
def test_case_class_factory(base_class: Type[T]) -> Type[T]:
class MyDerivedTestCase(base_class):
...
return MyDerivedTestCase
TracebaseTestCase = test_case_class_factory(TestCase)
TracebaseTransactionTestCase = test_case_class_factory(TransactionTestCase)
As soon as I removed the type hints from the declared variables (TracebaseTestCase
and TracebaseTransactionTestCase
), they went from blue to green, and the syntax highlighting started working in subsequent derived classes!
I realized that setting the factory method to output -> Type[TestCase]
, was static, and that I could use a TypeVar
to make it dynamic. I'm not sure I totally understand all of this. For example, I don't know what the significance of the first argument to TypeVar
is, but I think I learned one thing:
"TracebaseTestCase: TestCase = ...
" was wrong. It was type-hinting TracebaseTestCase
as an instance of TestCase
, not a type/class. That's why it was highlighted blue.
And I'm not sure I understand if VSCode could know what type TracebaseTestCase
is if I don't type-hint it: TracebaseTransactionTestCase = test_case_class_factory(TransactionTestCase)
. Can it? I.e. Can it differentiate between the presence of assertQuerySetEqual
as a method of the class?