-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Activate strict mode and fix type hints #220
base: main
Are you sure you want to change the base?
Conversation
880b6aa
to
29dd2e7
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #220 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 39 39
Lines 2417 2455 +38
Branches 322 331 +9
=========================================
+ Hits 2417 2455 +38 ☔ View full report in Codecov by Sentry. |
b137179
to
76e5157
Compare
76e5157
to
e84189c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! That must have been long and painful.
I really like the MetadataBase
and the get_reserved_names
; very nice!
I dont mind the add_metata()
, it was done on purpose.
My only concern regards accepting str
for paths. We should not go this way IMO.
A way to resolve it might be to create a custom context manager like
from pathlib import Path
from tempfile import TemporaryDirectory
@contextmanager
def path_from(path: Path | TemporaryDirectory | str):
if isinstance(path, Path):
yield path
elif callable(hasattr(path, "__enter__")):
with path as pathname:
yield Path(pathname)
else:
yield Path(pathname)
You would use it like:
with path_from(existing_tmp_path or tempfile.TemporaryDirectory()) as tmp_dir:
...
fbc459e
to
0d1a8e2
Compare
0d1a8e2
to
78fa538
Compare
Fix #212
Changes:
pyright: ignore[...]
hints (always qualified)from __future__ import annotations
since we target only Python 3.12, those are useless nowwith_process
argument ofzimscraperlib.video.encoding.reencode
and always return the process ; those who don't mind about this second argument can just drop it, this simplify the codebase and the signature and the type hintszimscraperlib.download.get_retry_adapter
since it is used inzimscraperlib.zim.provider
__all__
inzimscraperlib/video/__init__.py
(import where there but not used hence mostly useless)mappings
attribute ofzimscraperlib.video.config.Config
:mappings
values are always a string since they are in fact keys)options
anddefaults
attributes ofzimscraperlib.video.config.Config
:options
anddefaults
values are always a string or None since they are intended to be passed to the CLI as argument ; authorizing int and bool is not properly handled in the code, not needed, not used.MetadataBase
class which holds most property and can accomodate anyvalue
type, but obviously is abstract because it has no idea how to transform any value into a libzim value (this is the purpose of real implementation)AnyMetadata
type aliasU
to properly type all class decorators so that they properly accommodate to the class they are used on while still knowing it has access to all attributes of theMetadataBase
class (it took me ages to figure this out, so worth mentioning and have in mind)get_reserved_names
method which broke for some reason ; I feel like new implementation is indeed simpler (it does not need to rely on the weirdglobals()
)_log_metadata
and drop theis None
test ; type checker was complaining that this is not possible to happen (which is True if scraper use type hints properly and/or use only public methods which are protected by beartype ... only case where it can happen is if you do not use type checker and you manipulate directly the_metadata
attribute ...) and message logged is not awful in such a situation... is improper metadata type: NoneType: None
instead of... is None
Nota:
zimscraperlib.zim.creator.Creator
with methodsadd_metadata
andadd_item
. I had to set the pragma# pyright: ignore[reportIncompatibleMethodOverride]
because these methods override the base class methods (from pylibzim) with incompatible signature. This is not recommended. I feel like it is still acceptable since very convenient, and not a big deal (we never use the base class directly in scrapers in fact, and we do not have multiple child implementation of the base class with different signatures, so this is not really confusing).