Commit Graph

102 Commits

Author SHA1 Message Date
Karim shoair e785cf26ce fix(parser)!: Optimize parser for repeated operations 2026-02-14 03:34:13 +02:00
Karim shoair a62330d2f1 fix: add typed overloads to Selectors.get() for proper default type inference
Type checkers now correctly infer the return type based on the default value:
- .get() → TextHandler | None
- .get("") → TextHandler | str
- .get(0) → TextHandler | int
2026-02-13 17:30:55 +02:00
Karim shoair 5ec929435b style: Fix all mypy errors and add type hints to untyped function bodies
**Resolved all 65 mypy errors across 14 files and added type annotations to all previously untyped function bodies. Final result: 0 errors with --check-untyped-defs enabled, all 454 tests pass.**

`scrapling/core/_types.py`

  - Removed broken Self = object fallback — now requires typing_extensions for Python < 3.11

`scrapling/core/storage.py`

  - Fixed str/bytes mismatch in _get_hash() — used separate _identifier_bytes variable instead of reassigning from str to bytes

`scrapling/core/custom_types.py`

  - split() return type: Union[List, "TextHandlers"] → list[Any] (avoids LSP violation with parent list[str])
  - format() kwargs: **kwargs: str → **kwargs: object (matches parent str.format signature)
  - AttributesHandler.__init__: Added mapping: Any = None, **kwargs: Any and -> None
  - json_string property: Added -> bytes return type

`scrapling/core/mixins.py`

  - Changed self: "Selector" to self: Any on all mixin methods (mypy can't handle forward-reference self types on non-subclass mixins)
  - Added Dict[str, int] annotation for counter variable
  - Removed unused TYPE_CHECKING / Selector imports

`scrapling/parser.py (~30 errors)`

  - Added body: str | bytes pre-annotation for dual-type if/elif assignment
  - Used Dict[str, Any] kwargs dict for HTMLParser(...) to bypass incomplete lxml stubs missing default_doctype
  - Changed base_url=url or None → base_url=url or "" (avoids str | None vs str | bytes)
  - bool(adaptive) to guarantee bool type for __adaptive_enabled
  - Declared __text: Optional[TextHandler], __tag: Optional[str], __attributes: Optional[AttributesHandler] at top of __init__
  - cast(List, ...) for all XPath() call results (_find_all_elements, _find_all_elements_with_spaces)
  - Added Dict[float, List[Any]] for score_table, Dict[str, Any] for attributes
  - Changed score, checks = 0, 0 → score: float = 0; checks: int = 0 (two locations)
  - Renamed target → target_element in save() to avoid variable redefinition with different types
  - Wrapped node_text.clean() / .lower() in TextHandler(...) to preserve type

`scrapling/engines/_browsers/_page.py`

  - Added PageInfo[SyncPage] | PageInfo[AsyncPage] union type annotation to page_info variable

`scrapling/engines/_browsers/_validators.py`

  - Convert method_kwargs (TypedDict) to plain Dict[str, Any] before dynamic key access

`scrapling/engines/_browsers/_base.py`

  - Added _config declaration to BaseSessionMixin
  - Used cast(StealthConfig, self._config) in __generate_stealth_options to access stealth-only attributes
  - Added Tuple[str, ...] annotation for flags
  - Removed redundant narrower StealthConfig type annotation on self._config in StealthySessionMixin.__validate__
  - Widened SyncSession and AsyncSession fields (playwright, context, browser) to Any to support both playwright and patchright types
  - Added -> None to both start() methods

`scrapling/engines/_browsers/_stealth.py`

  - Added Optional, ProxyType imports
  - Annotated proxy: Optional[ProxyType] in both sync/async fetch loops
  - Annotated outer_box: Any at first declaration, removed duplicate type annotations in subsequent branches
  - Added -> None to sync and async start()
  - Added config: Any parameter type to _initialize_context
  - Removed redundant self.context: AsyncBrowserContext re-annotations in conditional branches

`scrapling/engines/_browsers/_controllers.py`

  - Added Optional, ProxyType imports
  - Annotated proxy: Optional[ProxyType] in both sync/async fetch loops
  - Added -> None to async start()
  - Removed redundant self.context: AsyncBrowserContext re-annotations

`scrapling/spiders/request.py`

  - Added Optional import, typed _fp: Optional[bytes] = None
  - Removed redundant body: bytes re-annotation

`scrapling/spiders/session.py`

  - Used separate client variable instead of reassigning session = session._client (avoids type incompatibility and fixes a bug where session._make_request was called instead of client._make_request)
  - Added -> None to SessionManager.__init__

`scrapling/engines/toolbelt/convertor.py`

  - Added list[Response] annotation for history in both sync/async methods

`scrapling/engines/static.py`

  - FetcherClient.__init__ and AsyncFetcherClient.__init__: Added **kwargs: Any and -> None

`scrapling/core/shell.py`

  - Wrapped re_sub(...) result in TextHandler(...) to maintain correct type
  - Added -> None to CurlParser.__init__
  - Added full type signature to create_wrapper, replaced wrapper.__signature__ = ... with setattr(wrapper, "__signature__", ...) to satisfy mypy
  - Added Callable to imports
2026-02-07 16:30:00 +02:00
Karim shoair d00c34ee7f style(parser): Improve the type hint for find_by_text and find_by_regex 2026-02-06 03:19:00 +02:00
Karim shoair aa7a95fb70 feat(parser)!: Make all selection return selector objects by default
- The strings/Texthandlers are only returned by `get`/`getall`/`extract`/`extract_first`. This makes the type checking/autocompletion experience consistent.
- Removed `css_first` and `xpath_first` since it doesn't make sense to leave them now.
- Made the type hints more accurate in multiple places.
2026-02-06 02:43:51 +02:00
Karim shoair dac854a129 fix(parser): handle responses with empty body
It makes root's value None
2026-01-13 02:35:26 +02:00
Karim shoair ae719a9d54 fix(parser): Improve response to json conversion 2025-12-17 00:15:55 +02:00
Karim shoair 170205599d fix(parser): Better approach for web pages where the encoding is not always correctly declared
Fixes #110 and avoids defaulting to a specific encoding like #111
2025-11-22 20:24:55 +02:00
Karim shoair e20882af5d style: removing dead code from the parser 2025-11-08 20:08:23 +02:00
Karim shoair debe03256b refactor: Making all the codebase acceptable by PyRight
Also fixes #97
2025-10-05 04:03:39 +03:00
Karim shoair 3da806210b perf: General code restructure to not use more than needed memory 2025-09-23 18:34:54 +03:00
Karim shoair 63a46a8e0b fix(parser): An encoding issue with converting bytes to string on some encoding types
Removing that `invalid start byte` annoying bug
2025-09-23 18:32:56 +03:00
Karim shoair a9d05cc0ef style: Removing dead code/docstrings and correcting type hints 2025-09-19 04:49:30 +03:00
Karim shoair b197f8a5f9 fix(parser): Improve selectors re function 2025-09-18 13:56:37 +03:00
Karim shoair 570303f938 fix(parser): Make html_content and prettify return strings not bytes (depends on the encoding) 2025-09-16 01:58:51 +03:00
Karim shoair ce4fc31f2c feat: Make .body return the passed content as it is without any processing
This makes it possible to download files and deal with non-HTML requests (ex: #81 )
2025-09-14 20:21:45 +03:00
Karim shoair 67ca139ff9 fix: Fixes for multiple encoding issues (#80 & #81 ) 2025-09-14 20:20:29 +03:00
Karim shoair 13d7e70cb7 refactor: Make all fetchers as an optional dependency group
+ Removing some dead code
2025-09-13 16:07:48 +03:00
Karim shoair 330d03559c style: applying the new ruff rules to all files 2025-09-13 03:22:53 +03:00
Karim shoair cf57af588e docs: update doc strings with correct naming 2025-08-25 06:08:15 +03:00
Karim shoair 2f402f4835 style: add flags for tests coverage
- Some are already tested but the coverage report can't see it.
- Some are not necessary to test or too hard to test on GitHub's CI
2025-08-17 01:02:14 +03:00
Karim shoair 84337ef1ac fix(parser): count all nested children of ignored tags in get_all_text 2025-08-16 14:24:51 +03:00
Karim shoair 740ae815c3 perf: speeding up find_by_text and find_by_regex by 3% 2025-08-02 19:46:18 +03:00
Karim shoair 67658f1723 perf: Speeding up below_elements and relocate by 3% 2025-08-02 19:45:43 +03:00
Karim shoair d1a2ecd341 perf: optimize get_all_text and adaptive logic by another 10% 2025-08-02 04:08:14 +03:00
Karim shoair 1ec6f0e0f0 perf: optimizing find_similar method 2025-08-02 03:39:05 +03:00
Karim shoair aec4889d25 perf: Optimizing next and previous properties 2025-08-02 03:37:48 +03:00
Karim shoair 548a40d850 perf: speed up get_all_text function by another 20% 2025-08-02 03:36:59 +03:00
Karim shoair 4c4202daae perf(parser): Speeding up css_first and xpath_first than normal ones 2025-08-01 16:41:09 +03:00
Karim shoair 83a19f3b17 perf(parser): A lot of optimizations to speed things up 2025-08-01 06:28:52 +03:00
Karim shoair df48662c00 perf(parser): A lot of optimizations to speed things up 2025-08-01 05:42:32 +03:00
Karim shoair af5f3688c1 fix: moving types to use Union again 2025-07-30 05:39:25 +03:00
Karim shoair 32cb76604c style: Adjustments to the translator 2025-07-30 03:28:16 +03:00
Karim shoair 5bb1266fa5 style: using isinstance function as the main way for type checking 2025-07-30 03:14:23 +03:00
Karim shoair ae9ccaec79 style: A lot of type hints correction
Since we are using Py3.10 as minimum version now, we remove Union when possible
2025-07-30 02:46:57 +03:00
Karim shoair 9415acccce fix: shortcuts for backward compatibility 2025-07-30 01:50:48 +03:00
Karim shoair 7300efa77e style(parser): optimize selectors instances creation 2025-07-30 01:45:26 +03:00
Karim shoair e7cdd39695 style: replacing os with Pathlib and small optimizations 2025-07-30 01:15:28 +03:00
Karim shoair 9bcb9e9d93 style: General type hints fixes and imports optimizing 2025-07-29 23:43:06 +03:00
Karim shoair 715dfb4243 feat: add length property to Selectors to write less code 2025-07-29 06:19:45 +03:00
Karim shoair 29b77a96c8 refactor(parser): optimize imports 2025-07-29 06:18:12 +03:00
Karim shoair b9c7a5af2e refactor: replace's Selector inpt (text/body) with 1 argument called content 2025-07-29 06:08:15 +03:00
Karim shoair 264ae02aa7 refactor: huge change, many features/class got a better naming
- `Adaptor` became `Selector`
- `Adaptors` became `Selectors`
- `auto_match` argument/feature became `adaptive`
- `adaptor_arguments` argument became `selector_config`
- `automatch_domain` argument became `adaptive_domain`
- `additional_arguments` argument became `additional_args`
- `storage_adaptors` file became just `storage`
2025-07-29 04:20:23 +03:00
Karim shoair a5e4b91653 refactor: remove clean up function for Adaptor + make adaptor attributes accessible directly 2025-07-28 14:48:22 +03:00
Karim shoair 297e14230b refactor(parser): Multiple optimizations and fixes 2025-07-28 03:32:22 +03:00
Karim shoair d220050160 refactor(parser): Make get_all_text method 40% faster 2025-07-28 03:29:13 +03:00
Karim shoair b60fcbb884 refactor(Adaptor): Cleaner approach to find_similar method
This code is slower than before by about 2-5μs, but it's worth it.
2025-07-27 23:34:22 +03:00
Karim shoair ffde7e8ad9 fix(Adaptor): Add cleanup function to handle possible memory leak 2025-07-27 23:31:55 +03:00
Karim shoair 6ae1810405 docs: improve All Adaptor class doc strings 2025-07-27 22:41:37 +03:00
Karim shoair 93bd131bb6 fix(parser): Solve the ignored elements children issue while keeping speed
solves #61
2025-07-27 05:34:48 +03:00