55import os
66import tempfile
77import unittest
8+ from typing import List
89from pylint import lint
910from pylint .testutils import GenericTestReporter
1011
1112class ModuleShadowingTest (unittest .TestCase ):
1213
13- def setUp (self ):
14+ def setUp (self ) -> None :
1415 self .tempdir = tempfile .TemporaryDirectory ()
16+ self .addCleanup (self .tempdir .cleanup ) # Automatically cleanup tempdir
1517 self .pkg_dir = os .path .join (self .tempdir .name , "my_module" )
1618 os .makedirs (self .pkg_dir )
1719
@@ -24,10 +26,7 @@ def setUp(self):
2426
2527 self .test_file = os .path .join (self .tempdir .name , "main.py" )
2628
27- def tearDown (self ):
28- self .tempdir .cleanup ()
29-
30- def _run_pylint (self , code ):
29+ def _run_pylint (self , code : str ) -> List :
3130 with open (self .test_file , "w" , encoding = "utf-8" ) as f :
3231 f .write (code )
3332
@@ -45,13 +44,13 @@ def _run_pylint(self, code):
4544 )
4645 return reporter .messages
4746
48- def test_shadowed_format_call (self ):
47+ def test_shadowed_format_call (self ) -> None :
4948 code = "import my_module.utils as my_module\n my_module.format()\n "
5049 messages = self ._run_pylint (code )
5150 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
5251 self .assertEqual (len (errors ), 0 )
5352
54- def test_early_module_yield (self ):
53+ def test_early_module_yield (self ) -> None :
5554 code = """
5655 import my_module.utils as my_module
5756 x = my_module.other_method()
@@ -61,7 +60,7 @@ def test_early_module_yield(self):
6160 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
6261 self .assertEqual (len (errors ), 0 )
6362
64- def test_nested_shadowed_imports (self ):
63+ def test_nested_shadowed_imports (self ) -> None :
6564 code = """
6665 import my_module.submodule.utils as my_module
6766 my_module.format()
@@ -70,23 +69,24 @@ def test_nested_shadowed_imports(self):
7069 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
7170 self .assertEqual (len (errors ), 0 )
7271
73- def test_access_module_dict (self ):
72+ def test_access_module_dict (self ) -> None :
7473 code = "import my_module.utils as utils\n print(utils.__dict__)\n "
7574 messages = self ._run_pylint (code )
7675 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
7776 self .assertEqual (len (errors ), 0 )
7877
79- def test_non_shadowed_import (self ):
78+ def test_non_shadowed_import (self ) -> None :
8079 code = "import my_module.utils as utils\n utils.format()\n "
8180 messages = self ._run_pylint (code )
8281 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
8382 self .assertEqual (len (errors ), 0 )
8483
85- def test_shadowed_import_without_call (self ):
84+ def test_shadowed_import_without_call (self ) -> None :
8685 code = "import my_module.utils as my_module\n "
8786 messages = self ._run_pylint (code )
8887 errors = [msg for msg in messages if msg .msg_id == "E0611" ]
8988 self .assertEqual (len (errors ), 0 )
9089
9190if __name__ == "__main__" :
9291 unittest .main ()
92+
0 commit comments