Home > database >  Unresolved reference when declaring variable type from inner class
Unresolved reference when declaring variable type from inner class

Time:10-14

I have the following class: TestOne, TestTwo, TestThree, and TestFour. The class TestTwo, TestThree, and TestFour are inner class of TestOne. In TestThree, i used isinstance(test_three_input, TestOne.TestTwo) without error, however in TestFour, i tried to declare that the variable type should be TestTwo, but it gave me unresolved reference error.

class TestOne:
    class TestTwo:
        def __init__(self):
            pass

    class TestThree:
        def __init__(self, test_three_input):
            if isinstance(test_three_input, TestOne.TestTwo):
                print("yes")
            else:
                print("no")

    class TestFour:
        def __init__(self, test_four_input: TestOne.TestTwo):  # unresolved reference error
            pass 

The error is Unresolved reference 'TestOne'. What is the reason behind this error and how to fix this ?

CodePudding user response:

You need to use PEP 563:

from __future__ import annotations

to delay evaluation of annotations. This works for python 3.7 . It was scheduled to become the default behavior in python 3.10, but is delayed to later releases beyond 3.11.

  • Related