-
Notifications
You must be signed in to change notification settings - Fork 718
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
Improve converters and type annotations #1014
Conversation
I'm not sure how to best type |
I diffed the current docs against the RTD build for this PR and the changes look good. For example, |
But somehow I made sphinx-autodoc-typehints unhappy -- https://apscheduler--1014.org.readthedocs.build/en/1014/api.html#apscheduler.triggers.interval.IntervalTrigger looks like this: The types aren't in code blocks and don't link to the Python docs anymore. No idea what happened. |
as_timezone()
None
handling
def as_enum(enum_class: type[Enum]) -> Callable[[Enum | str], Enum]:
def converter(value: Enum | str) -> Enum: ... but it breaks the docs build (both locally and on RTD):
So I'll undo the type annotations but keep the change from |
The type annotations for converters are not appropriate, as converters are run before validators, so the input value could be literally anything. |
I'm not sure I follow. Type annotations should describe what's intended, so your type checker can catch errors before you get a |
Sure, but does the type checker know that it should look at the |
Absolutely. I guess the attrs docs aren't the clearest about this, but a field's type annotation is overridden by that of its converter. https://www.attrs.org/en/stable/init.html#converters says:
Which means that currently, converted fields are typed as After this PR: |
Since you've verified that these changes improve the type checking and documentation, I'm merging this. Thanks! |
Changes
Any
as_timezone
: Remove unreachableNone
handling (fixes UnreachableNone
handling inas_timezone()
converter #1013)None
to do the same thing as the"local"
default while being less explicit, so I omitted it from the type annotation and dropped the unreachable codeCronTrigger.from_crontab
: Swaptimezone
parameter type annotation ordering totzinfo | str
for consistencystr | tzinfo
as_enum
: UseEnum.__getitem__
instead ofEnum.__members__.__getitem__
Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/
) added which would fail without your patchdocs/
, in case of behavior changes or newfeatures)
docs/versionhistory.rst
).If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.
Updating the changelog
If there are no entries after the last release, use
**UNRELEASED**
as the version.If, say, your patch fixes issue #999, the entry should look like this:
* Fix big bad boo-boo in the async scheduler (#999 <https://github.com/agronholm/apscheduler/issues/999>_; PR by @yourgithubaccount)
If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.