Skip to content

pyright errors when using DoesNotExist #1274

@sirianni

Description

@sirianni

Running pyright on this code results in an error

"Type[Unknown]" is not a valid exception class
(variable) DoesNotExist: Type[Unknown]
    class WidgetModel(Model):
        class Meta:  # pyright: ignore[reportIncompatibleVariableOverride]
            table_name = "widgets"
            read_capacity_units = 1
            write_capacity_units = 1

        id = UnicodeAttribute(hash_key=True)
        name = UnicodeAttribute()

    WidgetModel.create_table(wait=True)

    try:
        WidgetModel.get("id")
    except WidgetModel.DoesNotExist:
        pytest.fail("WidgetModel does not exist")

Perhaps the aliasing of the type name is confusing pyright.

DoesNotExist: Type[DoesNotExist] = DoesNotExist

Activity

ikonst

ikonst commented on Jun 13, 2025

@ikonst
Contributor

Hmm. I see.

I think the issue is that pyright doesn't like the DoesNotExist in Type[DoesNotExist] seemingly self-referential to the variable name.

Doing this appears to fix it:

-DoesNotExist: Type[DoesNotExist] = DoesNotExist
+DoesNotExist: Type['DoesNotExist'] = DoesNotExist

Mind checking it?

https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqiApgDRQjEBuxAhgDYD68CxAUGwMb20DOvUACJhivAHJgYAUQAeSXjAAUszsQQwkYFAEoAXGyiGoCPrw5ce-KAFkwAE2L19RoSPGTZ8mLsIkA2gDkwqIS0nIKAQC6UAC8riEe4TDmMCBwzkYm-GzEMqrqNvaOAHTB7mFeUHxQxBmGlDQMzCSKxNocQA

sirianni

sirianni commented on Jun 13, 2025

@sirianni
Author

Yes, if I make that change to models.py and rerun pyright it seems to pass. Thanks for the quick response!

Importing the base DoesNotExist as a different name would also likely fix the issue.

linked a pull request that will close this issue on Jun 13, 2025
ikonst

ikonst commented on Jun 13, 2025

@ikonst
Contributor

Let's fix it.

Also, I've created a PR to add pyright to CI: #1276. So far seeing lots of errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @sirianni@ikonst

      Issue actions

        pyright errors when using DoesNotExist · Issue #1274 · pynamodb/PynamoDB