duo-labs / py_webauthn

Pythonic WebAuthn 🐍
https://duo-labs.github.io/py_webauthn
BSD 3-Clause "New" or "Revised" License
856 stars 171 forks source link

feat/improve-bytes-subclass-mypy-support #130

Closed MasterKale closed 2 years ago

MasterKale commented 2 years ago

Attempting to use the latest py_webauthn in a codebase featuring stricter use of mypy than I'd set up here revealed issues with the use of BytesLike as a model field type. Namely, otherwise valid values like bytes and memoryview were not specifically instances of BytesLike, and mypy thus deemed them inappropriate input:

Screen Shot 2022-04-27 at 11 03 15 AM

After some internal discussion with colleagues I chose to try and refactor the concept of BytesLike into types that would appease mypy while also supporting these more advanced bytes-adjacent values. This would aim to restore the ability to define model fields as bytes type, which is the type I otherwise want to assign.

This PR adds a new field validator to WebAuthnBaseModel that performs the coercion to bytes that was previously taken care of with BytesLike. This should maintain the core ability to pass in raw bytes values while defining Pydantic models in a way that makes mypy happy.

I've also updated this project's mypy config to help catch issues like this in the future.