-
Notifications
You must be signed in to change notification settings - Fork 57
Add type annotations #123
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
Add type annotations #123
Changes from all commits
4bb3f90
226ed10
d0300bb
9001f79
42beb06
0be9870
5833326
2cdd669
f79fa36
6ba0c82
d364823
39ea8c1
803c2fc
c782e2f
85e3e8b
eae8d2e
5749296
bea135f
d1dfc65
aca3c5d
840a196
288920f
60dcb44
c61b3e3
a75a223
0cfcfba
b66dc7f
9fc0e29
d15e818
7717874
622e783
fa37a44
766807b
14d4adf
bf9e19a
58198a2
dad87b0
408c14e
2bf5021
0d87e5b
d3ea107
8bd78b0
44e9635
0cc874c
54dcec4
5747886
bd15325
495519e
db4fe6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
import sys | ||||||
from functools import partial | ||||||
from typing import TYPE_CHECKING | ||||||
|
||||||
from . import _ipykernel | ||||||
from . import cli | ||||||
from . import cookies | ||||||
from . import examples | ||||||
|
@@ -16,8 +16,28 @@ | |||||
from .get import get_day_and_year | ||||||
from .post import submit as _impartial_submit | ||||||
|
||||||
__all__ = [ | ||||||
"AocdError", | ||||||
"cli", | ||||||
"cookies", | ||||||
"data", | ||||||
"examples", | ||||||
"exceptions", | ||||||
"get_data", | ||||||
"get", | ||||||
"models", | ||||||
"post", | ||||||
"runner", | ||||||
"submit", | ||||||
"utils", | ||||||
] | ||||||
|
||||||
def __getattr__(name): | ||||||
data: str | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
submit = _impartial_submit | ||||||
|
||||||
def __getattr__(name): # type: ignore[no-untyped-def] # no idea how to provide meaningful types here | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use
Suggested change
(make sure to import |
||||||
if name == "data": | ||||||
day, year = get_day_and_year() | ||||||
return get_data(day=day, year=year) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
from importlib.metadata import entry_points | ||
|
||
# typing.Self added in 3.11 | ||
if sys.version_info >= (3, 11): | ||
from typing import Self as Self # import using same name to tell the type checker we intend to export this (so other modules can import it) | ||
else: | ||
from typing_extensions import Self as Self # import using same name to tell the type checker we intend to export this (so other modules can import it) | ||
Comment on lines
+5
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is overkill. Just use
advent-of-code could instead use pyupgrade to keep the codebase clean; if in future the minimum release for the project is moved to Python 3.11, that tool would automatically move the import from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. As well as moving the import, is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pyupgrade won't (dependency management is out of scope), but a tool like deptry can be used to flag obsolete dependencies (as well as flag missing deps). |
||
|
||
# typing.ParamSpec added in 3.10 | ||
if sys.version_info >= (3, 10): | ||
from typing import ParamSpec as ParamSpec # import using same name to tell the type checker we intend to export this (so other modules can import it) | ||
else: | ||
from typing_extensions import ParamSpec as ParamSpec # import using same name to tell the type checker we intend to export this (so other modules can import it) | ||
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here. Just use |
||
|
||
# importlib.metadata.EntryPoints added in 3.10 | ||
if sys.version_info >= (3, 10): | ||
from importlib.metadata import EntryPoints | ||
|
||
# Python 3.10+ - group/name selectable entry points | ||
def get_entry_points(group: str, name: str) -> EntryPoints: | ||
return entry_points().select(group=group, name=name) | ||
|
||
def get_plugins(group: str = "adventofcode.user") -> EntryPoints: | ||
""" | ||
Currently installed plugins for user solves. | ||
""" | ||
return entry_points(group=group) | ||
else: | ||
from importlib.metadata import EntryPoint | ||
|
||
# Python 3.9 - dict interface | ||
def get_entry_points(group: str, name: str) -> list[EntryPoint]: | ||
return [ep for ep in entry_points()[group] if ep.name == name] | ||
|
||
def get_plugins(group: str = "adventofcode.user") -> list[EntryPoint]: | ||
""" | ||
Currently installed plugins for user solves. | ||
""" | ||
return entry_points().get(group, []) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from decimal import Decimal | ||
from fractions import Fraction | ||
from typing import Literal | ||
from typing import Optional | ||
from typing import TYPE_CHECKING | ||
from typing import Union | ||
|
||
if TYPE_CHECKING: | ||
import numpy as np | ||
|
||
__all__ = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why give these objects private names and then export them? The types would be useful for consumers of the library too; e.g. some 3rd party library might use: from aocd.types import Answer, Part
def process_answer(answer: Answer, part: Part | None = None, **kwargs: Any) -> None:
"""Process an AOC answer before submitting"""
... and you may want to store some of these types:
Note that the |
||
"_Answer", | ||
"_Part", | ||
"_LoosePart", | ||
] | ||
|
||
_Answer = Optional[Union[str, bytes, int, float, complex, Fraction, Decimal, "np.number[np.typing.NBitBase]"]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the number types are (virtual) subclasses of
Even if this wasn't the case, I'd have used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For posterity, using |
||
_Part = Literal["a", "b"] | ||
_LoosePart = Union[_Part, Literal[1, "1", 2, "2"]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have my reservations about the use of |
Uh oh!
There was an error while loading. Please reload this page.