Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better type checking for XBlock fields #710

Closed
kdmccormick opened this issue Jan 16, 2024 · 1 comment
Closed

Better type checking for XBlock fields #710

kdmccormick opened this issue Jan 16, 2024 · 1 comment
Labels
duplicate This issue or pull request already exists elsewhere

Comments

@kdmccormick
Copy link
Member

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.

@kdmccormick
Copy link
Member Author

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.

@kdmccormick kdmccormick closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2024
@kdmccormick kdmccormick added the duplicate This issue or pull request already exists elsewhere label Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists elsewhere
Projects
None yet
Development

No branches or pull requests

1 participant