[Python] pylance reports access attribute warning while it's checked

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    try:
    None
    except Exception as e:
    status_code = "error"
    # Try to extract status from exception
    if hasattr(e, "status_code"):
    status_code = str(e.status_code)


    PyLance throws the following warning:

    Cannot access attribute "status_code" for class "Exception"


    I use the "standard" type checking mode.

    Why is this warning thrown and how can I remove it without explicitly using a type ignore?

    Continue reading...

Compartilhe esta Página