Skip to content

Better type checking for XBlock fields #710

Closed as not planned
Closed as not planned

Description

@kdmccormick

Given a class like this:

class MyBlock(XBlock):
    my_bool = xblock.fields.Boolean(...)

I want mypy to understand this:

type(MyBlock.my_bool)               # --> xblock.fields.Boolean
type(instance_of_my_block.my_bool)  # --> bool

We can kinda hack around it right now by doing this:

class MyBlock(XBlock):
    my_bool: bool = xblock.fields.Boolean(...)

but that annotation isn't entirely correct, because the class-level attribute will always be an instance of xblock.fields.Field.

We should see what django-stubs does in order to make this work for Django models, which are in a similar situation.

Activity

  1. kdmccormick commented on Jan 31, 2024

    @kdmccormick
    MemberAuthor

    I'm folding this into:

    because I can't see how to add useful type hints to XBlock without making the fields behave correctly.

    Fortunately, I think this should be doable, because XBlock fields are implemented using the descriptor protocol, which mypy is awesome enough to understand 🔥 I'm pretty sure that this is how Django does things, too.

  2. added
    duplicate
    on Jan 31, 2024
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

    duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions