@@ -465,5 +465,153 @@ PYI061 Use `None` rather than `Literal[None]`
46546579 | d : None | (Literal [None ] | None )
46646680 | e : None | ((None | Literal [None ]) | None ) | None
467467 | ^^^^
468+ 81 |
469+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
468470 |
469471help : Replace with ` None`
472+
473+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
474+ -- > PYI061 .py :83 :18
475+ |
476+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
477+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
478+ | ^^^^
479+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
480+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
481+ |
482+ help : Replace with ` Optional[Literal[...]]`
483+ - from typing import Literal , Union
484+ 1 + from typing import Literal , Union , Optional
485+ 2 |
486+ 3 |
487+ 4 | def func1 (arg1 : Literal [None ]):
488+ --------------------------------------------------------------------------------
489+ 80 | e : None | ((None | Literal [None ]) | None ) | None
490+ 81 |
491+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
492+ - print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
493+ 83 + print (Optional [Literal [1 ]].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
494+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
495+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
496+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
497+
498+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
499+ -- > PYI061 .py :84 :18
500+ |
501+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
502+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
503+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
504+ | ^^^^
505+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
506+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
507+ |
508+ help : Replace with ` Optional[Literal[...]]`
509+ - from typing import Literal , Union
510+ 1 + from typing import Literal , Union , Optional
511+ 2 |
512+ 3 |
513+ 4 | def func1 (arg1 : Literal [None ]):
514+ --------------------------------------------------------------------------------
515+ 81 |
516+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
517+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
518+ - print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
519+ 84 + print (Optional [Literal [1 ]].method ()) # Should become (Literal [1 ] | None ).method ()
520+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
521+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
522+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
523+
524+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
525+ -- > PYI061 .py :85 :18
526+ |
527+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
528+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
529+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
530+ | ^^^^
531+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
532+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
533+ |
534+ help : Replace with ` Optional[Literal[...]]`
535+ - from typing import Literal , Union
536+ 1 + from typing import Literal , Union , Optional
537+ 2 |
538+ 3 |
539+ 4 | def func1 (arg1 : Literal [None ]):
540+ --------------------------------------------------------------------------------
541+ 82 | # Test cases for operator precedence issue (https :// github.com/astral-sh/ruff/issues/20265)
542+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
543+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
544+ - print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
545+ 85 + print (Optional [Literal [1 ]][0 ]) # Should become (Literal [1 ] | None )[0 ]
546+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
547+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
548+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
549+
550+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
551+ -- > PYI061 .py :86 :18
552+ |
553+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
554+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
555+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
556+ | ^^^^
557+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
558+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
559+ |
560+ help : Replace with ` Optional[Literal[...]]`
561+ - from typing import Literal , Union
562+ 1 + from typing import Literal , Union , Optional
563+ 2 |
564+ 3 |
565+ 4 | def func1 (arg1 : Literal [None ]):
566+ --------------------------------------------------------------------------------
567+ 83 | print (Literal [1 , None ].__dict__ ) # Should become (Literal [1 ] | None ).__dict__
568+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
569+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
570+ - print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
571+ 86 + print (Optional [Literal [1 ]] + 1 ) # Should become (Literal [1 ] | None ) + 1
572+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
573+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
574+
575+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
576+ -- > PYI061 .py :87 :18
577+ |
578+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
579+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
580+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
581+ | ^^^^
582+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
583+ |
584+ help : Replace with ` Optional[Literal[...]]`
585+ - from typing import Literal , Union
586+ 1 + from typing import Literal , Union , Optional
587+ 2 |
588+ 3 |
589+ 4 | def func1 (arg1 : Literal [None ]):
590+ --------------------------------------------------------------------------------
591+ 84 | print (Literal [1 , None ].method ()) # Should become (Literal [1 ] | None ).method ()
592+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
593+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
594+ - print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
595+ 87 + print (Optional [Literal [1 ]] * 2 ) # Should become (Literal [1 ] | None ) * 2
596+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
597+
598+ PYI061 [* ] Use ` Optional[Literal[...]]` rather than ` Literal[None, ...]`
599+ -- > PYI061 .py :88 :19
600+ |
601+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
602+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
603+ 88 | print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
604+ | ^^^^
605+ |
606+ help : Replace with ` Optional[Literal[...]]`
607+ - from typing import Literal , Union
608+ 1 + from typing import Literal , Union , Optional
609+ 2 |
610+ 3 |
611+ 4 | def func1 (arg1 : Literal [None ]):
612+ --------------------------------------------------------------------------------
613+ 85 | print (Literal [1 , None ][0 ]) # Should become (Literal [1 ] | None )[0 ]
614+ 86 | print (Literal [1 , None ] + 1 ) # Should become (Literal [1 ] | None ) + 1
615+ 87 | print (Literal [1 , None ] * 2 ) # Should become (Literal [1 ] | None ) * 2
616+ - print ((Literal [1 , None ]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
617+ 88 + print ((Optional [Literal [1 ]]).__dict__ ) # Should become ((Literal [1 ] | None )).__dict__
0 commit comments