我如何获得VSCODE IntelliSense来使用我的Python class Factory?

问题描述 投票:0回答:1
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)
这些课程效果很好。 我已经使用了多年了,但是当我从任何一类继承时,VSCODE的Intellisense和语法突出显示从未起作用,例如:

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
是白色的:

我今天在类型提示方面尝试了一些事情,但我似乎不知道如何使其起作用。 如何从这些工厂创建的类中继承与VSCODE的Intellisense和语法突出显示? enter image description here ! 我做到了! 这就是我的工作方式:

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)

python visual-studio-code intellisense syntax-highlighting
1个回答
0
投票
TracebaseTestCase

TracebaseTransactionTestCase
)的类型提示之后,它们从蓝色到绿色,然后语法突出显示开始在随后的派生类中工作!
i我意识到将工厂方法设置为输出

-> Type[TestCase]

是静态的,并且我可以使用
TypeVar
来使其动态。  我不确定我完全理解所有这些。  例如,我不知道第一个论点对
TypeVar

是什么意义,但是我认为我学到了一件事:

TracebaseTestCase: TestCase = ...
”是错误的。  它是类型的模具
TracebaseTestCase
作为
TestCase

的实例,而不是类型/类。 这就是为什么它被突出显示为蓝色。

我不确定我是否知道Vscode是否知道什么类型是(如果我不正确)(如

TracebaseTestCase
正确)。 可以吗?
	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.