Karim shoair commited on
Commit
1543315
·
1 Parent(s): 20dd99a

fix: Fixes for the type checking in the main `init` file

Browse files
scrapling/__init__.py CHANGED
@@ -2,27 +2,37 @@ __author__ = "Karim Shoair (karim.shoair@pm.me)"
2
  __version__ = "0.3.6"
3
  __copyright__ = "Copyright (c) 2024 Karim Shoair"
4
 
 
5
 
6
- # A lightweight approach to create a lazy loader for each import for backward compatibility
7
- # This will reduces initial memory footprint significantly (only loads what's used)
8
- def __getattr__(name):
9
- lazy_imports = {
10
- "Fetcher": ("scrapling.fetchers", "Fetcher"),
11
- "Selector": ("scrapling.parser", "Selector"),
12
- "Selectors": ("scrapling.parser", "Selectors"),
13
- "AttributesHandler": ("scrapling.core.custom_types", "AttributesHandler"),
14
- "TextHandler": ("scrapling.core.custom_types", "TextHandler"),
15
- "AsyncFetcher": ("scrapling.fetchers", "AsyncFetcher"),
16
- "StealthyFetcher": ("scrapling.fetchers", "StealthyFetcher"),
17
- "DynamicFetcher": ("scrapling.fetchers", "DynamicFetcher"),
18
- }
19
-
20
- if name in lazy_imports:
21
- module_path, class_name = lazy_imports[name]
 
 
 
 
 
 
 
22
  module = __import__(module_path, fromlist=[class_name])
23
  return getattr(module, class_name)
24
  else:
25
- raise AttributeError(f"module 'scrapling' has no attribute '{name}'")
26
 
27
 
28
- __all__ = ["Selector", "Fetcher", "AsyncFetcher", "StealthyFetcher", "DynamicFetcher"]
 
 
 
2
  __version__ = "0.3.6"
3
  __copyright__ = "Copyright (c) 2024 Karim Shoair"
4
 
5
+ from typing import Any, TYPE_CHECKING
6
 
7
+ if TYPE_CHECKING:
8
+ from scrapling.parser import Selector, Selectors
9
+ from scrapling.core.custom_types import AttributesHandler, TextHandler
10
+ from scrapling.fetchers import Fetcher, AsyncFetcher, StealthyFetcher, DynamicFetcher
11
+
12
+
13
+ # Lazy import mapping
14
+ _LAZY_IMPORTS = {
15
+ "Fetcher": ("scrapling.fetchers", "Fetcher"),
16
+ "Selector": ("scrapling.parser", "Selector"),
17
+ "Selectors": ("scrapling.parser", "Selectors"),
18
+ "AttributesHandler": ("scrapling.core.custom_types", "AttributesHandler"),
19
+ "TextHandler": ("scrapling.core.custom_types", "TextHandler"),
20
+ "AsyncFetcher": ("scrapling.fetchers", "AsyncFetcher"),
21
+ "StealthyFetcher": ("scrapling.fetchers", "StealthyFetcher"),
22
+ "DynamicFetcher": ("scrapling.fetchers", "DynamicFetcher"),
23
+ }
24
+ __all__ = ["Selector", "Fetcher", "AsyncFetcher", "StealthyFetcher", "DynamicFetcher"]
25
+
26
+
27
+ def __getattr__(name: str) -> Any:
28
+ if name in _LAZY_IMPORTS:
29
+ module_path, class_name = _LAZY_IMPORTS[name]
30
  module = __import__(module_path, fromlist=[class_name])
31
  return getattr(module, class_name)
32
  else:
33
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
34
 
35
 
36
+ def __dir__() -> list[str]:
37
+ """Support for dir() and autocomplete."""
38
+ return sorted(__all__ + ["fetchers", "parser", "cli", "core", "__author__", "__version__", "__copyright__"])
scrapling/core/_types.py CHANGED
@@ -39,6 +39,4 @@ except ImportError: # pragma: no cover
39
  try:
40
  from typing_extensions import Self # Backport
41
  except ImportError:
42
- from typing import TypeVar
43
-
44
  Self = object
 
39
  try:
40
  from typing_extensions import Self # Backport
41
  except ImportError:
 
 
42
  Self = object
scrapling/engines/_browsers/__init__.py CHANGED
@@ -1,2 +0,0 @@
1
- from ._controllers import DynamicSession, AsyncDynamicSession
2
- from ._camoufox import StealthySession, AsyncStealthySession