Skip to content

Fix for redundant-none-literal (PYI061) is marked as safe but should not be #20729

@user27182

Description

@user27182

Summary

PYI061 is marked as a safe fix but it's not, and breaks code that uses typing.get_args to do input validation based on type hints. E.g. the two outputs from get_args are not the same before and after the fix

>>> from typing import Literal, get_args
>>> options = Literal['foo', 'bar', None]  # violates PYI061
>>> print(get_args(options))
('foo', 'bar', None)

>>> options = Literal['foo', 'bar'] | None  # auto-fix for PYI061
>>> print(get_args(options))
(typing.Literal['foo', 'bar'], <class 'NoneType'>)

This auto-fix breaks this code:

def thing(arg: options):
    # Validate input
    assert arg in get_args(options)
    # Do useful stuff
    ...

Version

v0.13.3 (from pre-commit mirror)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions