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

Stricter types #2

Merged
merged 2 commits into from
Jul 22, 2024
Merged

Conversation

CharString
Copy link
Contributor

Refactors:

  • looping over Member descriptors
  • calculate String length_encoding with a logarithm instead of a loop.

Makes things like this give a type error:

class NullTest(tlv.TLVStructure):
    n = tlv.BoolMember(None, nullable=True)
    notn = tlv.BoolMember(None, nullable=False)

s = NullTest()
s.n = None  # checks ok
s.notn = None  # doesn't type check

Doesn't handle StructMember yet.

Makes things like this give a type error:

```
class NullTest(tlv.TLVStructure):
    n = tlv.BoolMember(None, nullable=True)
    notn = tlv.BoolMember(None, nullable=False)

s = NullTest()
s.n = None  # checks ok
s.notn = None  # doesn't type check
```
@CharString
Copy link
Contributor Author

PS this typechecks in pyright, doesn't with mypy and I haven't checked pyre

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Thank you! I hadn't seen typing + descriptors before.

@tannewt tannewt merged commit 89c6a97 into adafruit:main Jul 22, 2024
3 checks passed
@CharString
Copy link
Contributor Author

CharString commented Jul 22, 2024

Very cool! Thank you! I hadn't seen typing + descriptors before.

@tannewt I haven't seen it done either. It was a bit of a puzzle. Annotating __new__ and returning Self[Optional[_T]] in case of optional ∨ nullable was a dead end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants