71
71
72
72
# Private typing symbols.
73
73
_T = typing .TypeVar ('_T' ) # Unbounded TypeVar for general usage
74
- _OutcomeType = unittest .case ._Outcome # pytype: disable=module-attr
75
74
_TEXT_OR_BINARY_TYPES = (str , bytes )
76
75
77
76
# Suppress surplus entries in AssertionError stack traces.
@@ -244,7 +243,7 @@ def wasSuccessful(self) -> bool:
244
243
test_result = unittest .TestResult ()
245
244
test_result .addUnexpectedSuccess (unittest .FunctionTestCase (lambda : None ))
246
245
if test_result .wasSuccessful (): # The bug is present.
247
- unittest .TestResult .wasSuccessful = wasSuccessful
246
+ unittest .TestResult .wasSuccessful = wasSuccessful # type: ignore[method-assign]
248
247
if test_result .wasSuccessful (): # Warn the user if our hot-fix failed.
249
248
sys .stderr .write ('unittest.result.TestResult monkey patch to report'
250
249
' unexpected passes as failures did not work.\n ' )
@@ -373,7 +372,7 @@ def _create(
373
372
cls ,
374
373
base_path : str ,
375
374
file_path : Optional [str ],
376
- content : AnyStr ,
375
+ content : Optional [ AnyStr ] ,
377
376
mode : str ,
378
377
encoding : str ,
379
378
errors : str ,
@@ -531,26 +530,32 @@ class _method(object):
531
530
(e.g. Cls.method(self, ...)) but is still situationally useful.
532
531
"""
533
532
533
+ _finstancemethod : Any
534
+ _fclassmethod : Optional [Any ]
535
+
534
536
def __init__ (self , finstancemethod : Callable [..., Any ]) -> None :
535
537
self ._finstancemethod = finstancemethod
536
538
self ._fclassmethod = None
537
539
538
540
def classmethod (self , fclassmethod : Callable [..., Any ]) -> '_method' :
539
- self ._fclassmethod = classmethod (fclassmethod )
541
+ if isinstance (fclassmethod , classmethod ):
542
+ self ._fclassmethod = fclassmethod
543
+ else :
544
+ self ._fclassmethod = classmethod (fclassmethod )
540
545
return self
541
546
542
- def __doc__ (self ) -> str :
543
- if getattr ( self . _finstancemethod , '__doc__' ):
544
- return self ._finstancemethod . __doc__
545
- elif getattr (self ._fclassmethod , '__doc__' ):
546
- return self . _fclassmethod . __doc__
547
- return ''
547
+ def __doc__ (self ) -> str : # type: ignore[override]
548
+ return (
549
+ getattr ( self ._finstancemethod , ' __doc__' )
550
+ or getattr (self ._fclassmethod , '__doc__' )
551
+ or ''
552
+ )
548
553
549
554
def __get__ (
550
555
self , obj : Optional [Any ], type_ : Optional [Type [Any ]]
551
556
) -> Callable [..., Any ]:
552
557
func = self ._fclassmethod if obj is None else self ._finstancemethod
553
- return func .__get__ (obj , type_ ) # pytype: disable= attribute-error
558
+ return func .__get__ (obj , type_ ) # type: ignore[ attribute-error]
554
559
555
560
556
561
class TestCase (unittest .TestCase ):
@@ -572,10 +577,10 @@ class TestCase(unittest.TestCase):
572
577
_exit_stack = None
573
578
_cls_exit_stack = None
574
579
575
- def __init__ (self , * args , ** kwargs ):
580
+ def __init__ (self , * args , ** kwargs ) -> None :
576
581
super (TestCase , self ).__init__ (* args , ** kwargs )
577
582
# This is to work around missing type stubs in unittest.pyi
578
- self ._outcome : Optional [_OutcomeType ] = getattr (self , '_outcome' )
583
+ self ._outcome : Optional [Any ] = getattr (self , '_outcome' )
579
584
580
585
def setUp (self ):
581
586
super (TestCase , self ).setUp ()
@@ -749,7 +754,8 @@ def enter_context(self, manager: ContextManager[_T]) -> _T:
749
754
return self ._exit_stack .enter_context (manager )
750
755
751
756
@enter_context .classmethod
752
- def enter_context (cls , manager : ContextManager [_T ]) -> _T : # pylint: disable=no-self-argument
757
+ @classmethod
758
+ def _enter_context_cls (cls , manager : ContextManager [_T ]) -> _T :
753
759
if sys .version_info >= (3 , 11 ):
754
760
return cls .enterClassContext (manager )
755
761
@@ -1355,7 +1361,7 @@ def assertRaisesWithPredicateMatch(
1355
1361
* args , ** kwargs ) -> None :
1356
1362
# The purpose of this return statement is to work around
1357
1363
# https://github.com/PyCQA/pylint/issues/5273; it is otherwise ignored.
1358
- return self ._AssertRaisesContext (None , None , None )
1364
+ return self ._AssertRaisesContext (None , None , None ) # type: ignore[return-value]
1359
1365
1360
1366
def assertRaisesWithPredicateMatch (self , expected_exception , predicate ,
1361
1367
callable_obj = None , * args , ** kwargs ):
@@ -1399,7 +1405,7 @@ def assertRaisesWithLiteralMatch(
1399
1405
callable_obj : Callable [..., Any ], * args , ** kwargs ) -> None :
1400
1406
# The purpose of this return statement is to work around
1401
1407
# https://github.com/PyCQA/pylint/issues/5273; it is otherwise ignored.
1402
- return self ._AssertRaisesContext (None , None , None )
1408
+ return self ._AssertRaisesContext (None , None , None ) # type: ignore[return-value]
1403
1409
1404
1410
def assertRaisesWithLiteralMatch (self , expected_exception ,
1405
1411
expected_exception_message ,
@@ -1782,7 +1788,7 @@ def assertDataclassEqual(self, first, second, msg=None):
1782
1788
'Cannot detect difference by examining the fields of the dataclass.'
1783
1789
)
1784
1790
1785
- raise self .fail ('\n ' .join (message ), msg )
1791
+ self .fail ('\n ' .join (message ), msg )
1786
1792
1787
1793
def assertUrlEqual (self , a , b , msg = None ):
1788
1794
"""Asserts that urls are equal, ignoring ordering of query params."""
@@ -1881,7 +1887,7 @@ def _getAssertEqualityFunc(
1881
1887
1882
1888
def fail (self , msg = None , user_msg = None ) -> NoReturn :
1883
1889
"""Fail immediately with the given standard message and user message."""
1884
- return super (TestCase , self ).fail (self ._formatMessage (user_msg , msg ))
1890
+ super (TestCase , self ).fail (self ._formatMessage (user_msg , msg ))
1885
1891
1886
1892
1887
1893
def _sorted_list_difference (
@@ -1909,12 +1915,12 @@ def _sorted_list_difference(
1909
1915
try :
1910
1916
e = expected [i ]
1911
1917
a = actual [j ]
1912
- if e < a :
1918
+ if e < a : # type: ignore[operator]
1913
1919
missing .append (e )
1914
1920
i += 1
1915
1921
while expected [i ] == e :
1916
1922
i += 1
1917
- elif e > a :
1923
+ elif e > a : # type: ignore[operator]
1918
1924
unexpected .append (a )
1919
1925
j += 1
1920
1926
while actual [j ] == a :
@@ -2204,12 +2210,12 @@ def _run_in_app(
2204
2210
# This must be a separate loop since multiple flag names (short_name=) can
2205
2211
# point to the same flag object.
2206
2212
for name in FLAGS :
2207
- FLAGS [name ].parse = noop_parse
2213
+ FLAGS [name ].parse = noop_parse # type: ignore[method-assign]
2208
2214
try :
2209
2215
argv = FLAGS (sys .argv )
2210
2216
finally :
2211
2217
for name in FLAGS :
2212
- FLAGS [name ].parse = stored_parse_methods [name ]
2218
+ FLAGS [name ].parse = stored_parse_methods [name ] # type: ignore[method-assign]
2213
2219
sys .stdout .flush ()
2214
2220
2215
2221
function (argv , args , kwargs )
@@ -2364,6 +2370,7 @@ def get_default_xml_output_filename() -> Optional[str]:
2364
2370
return os .path .join (
2365
2371
os .environ ['TEST_XMLOUTPUTDIR' ],
2366
2372
os .path .splitext (os .path .basename (sys .argv [0 ]))[0 ] + '.xml' )
2373
+ return None
2367
2374
2368
2375
2369
2376
def _setup_filtering (argv : MutableSequence [str ]) -> bool :
@@ -2490,7 +2497,7 @@ def getShardedTestCaseNames(testCaseClass):
2490
2497
filtered_names .append (testcase )
2491
2498
return [x for x in ordered_names if x in filtered_names ]
2492
2499
2493
- base_loader .getTestCaseNames = getShardedTestCaseNames
2500
+ base_loader .getTestCaseNames = getShardedTestCaseNames # type: ignore[method-assign]
2494
2501
return base_loader , shard_index
2495
2502
2496
2503
@@ -2559,9 +2566,12 @@ def _run_and_get_tests_result(
2559
2566
# XML file name is based upon (sorted by priority):
2560
2567
# --xml_output_file flag, XML_OUTPUT_FILE variable,
2561
2568
# TEST_XMLOUTPUTDIR variable or RUNNING_UNDER_TEST_DAEMON variable.
2562
- if not FLAGS .xml_output_file :
2563
- FLAGS .xml_output_file = get_default_xml_output_filename ()
2564
- xml_output_file = FLAGS .xml_output_file
2569
+ if FLAGS .xml_output_file :
2570
+ xml_output_file = FLAGS .xml_output_file
2571
+ else :
2572
+ xml_output_file = get_default_xml_output_filename ()
2573
+ if xml_output_file :
2574
+ FLAGS .xml_output_file = xml_output_file
2565
2575
2566
2576
xml_buffer = None
2567
2577
if xml_output_file :
0 commit comments