Skip to content

🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev

style: using `isinstance` function as the main way for type checking

Karim shoair5bb1266fa5eb
diff --git a/scrapling/core/custom_types.py b/scrapling/core/custom_types.py
index 6350a84..bc8ab74 100644
--- a/scrapling/core/custom_types.py
+++ b/scrapling/core/custom_types.py
@@ -310,7 +310,7 @@ class AttributesHandler(Mapping[str, _TextHandlerType]):
def __init__(self, mapping=None, **kwargs):
mapping = (
{
- key: TextHandler(value) if type(value) is str else value
+ key: TextHandler(value) if isinstance(value, str) else value
for key, value in mapping.items()
}
if mapping is not None
@@ -320,7 +320,7 @@ class AttributesHandler(Mapping[str, _TextHandlerType]):
if kwargs:
mapping.update(
{
- key: TextHandler(value) if type(value) is str else value
+ key: TextHandler(value) if isinstance(value, str) else value
for key, value in kwargs.items()
}
)
diff --git a/scrapling/core/storage.py b/scrapling/core/storage.py
index 9708568..096b688 100644
--- a/scrapling/core/storage.py
+++ b/scrapling/core/storage.py
@@ -22,7 +22,7 @@ class StorageSystemMixin(ABC):
@lru_cache(64, typed=True)
def _get_base_url(self, default_value: str = "default") -> str:
- if not self.url or type(self.url) is not str:
+ if not self.url or not isinstance(self.url, str):
return default_value
try:
diff --git a/scrapling/engines/toolbelt/convertor.py b/scrapling/engines/toolbelt/convertor.py
index dd0e8b1..b7da02a 100644
--- a/scrapling/engines/toolbelt/convertor.py
+++ b/scrapling/engines/toolbelt/convertor.py
@@ -240,7 +240,7 @@ class ResponseFactory:
return Response(
url=response.url,
content=response.content
- if type(response.content) is bytes
+ if isinstance(response.content, bytes)
else response.content.encode(),
status=response.status_code,
reason=response.reason,
diff --git a/scrapling/engines/toolbelt/custom.py b/scrapling/engines/toolbelt/custom.py
index ba095d7..79b52d1 100644
--- a/scrapling/engines/toolbelt/custom.py
+++ b/scrapling/engines/toolbelt/custom.py
@@ -216,7 +216,7 @@ class BaseFetcher:
storage_args=cls.storage_args,
)
if cls.adaptive_domain:
- if type(cls.adaptive_domain) is not str:
+ if not isinstance(cls.adaptive_domain, str):
log.warning(
'[Ignored] The argument "adaptive_domain" must be of string type'
)
diff --git a/scrapling/parser.py b/scrapling/parser.py
index 3e6cab9..219181d 100644
--- a/scrapling/parser.py
+++ b/scrapling/parser.py
@@ -734,11 +734,11 @@ class Selector(SelectorsGeneration):
# Brace yourself for a wonderful journey!
for arg in args:
- if type(arg) is str:
+ if isinstance(arg, str):
tags.add(arg)
- elif type(arg) in [list, tuple, set]:
- if not all(map(lambda x: type(x) is str, arg)):
+ elif type(arg) in (list, tuple, set):
+ if not all(map(lambda x: isinstance(x, str), arg)):
raise TypeError(
"Nested Iterables are not accepted, only iterables of tag names are accepted"
)
@@ -746,7 +746,10 @@ class Selector(SelectorsGeneration):
elif isinstance(arg, dict):
if not all(
- [(type(k) is str and type(v) is str) for k, v in arg.items()]
+ [
+ (isinstance(k, str) and isinstance(v, str))
+ for k, v in arg.items()
+ ]
):
raise TypeError(
"Nested dictionaries are not accepted, only string keys and string values are accepted"
@@ -769,7 +772,9 @@ class Selector(SelectorsGeneration):
f'Argument with type "{type(arg)}" is not accepted, please read the docs.'
)
- if not all([(type(k) is str and type(v) is str) for k, v in kwargs.items()]):
+ if not all(
+ [(isinstance(k, str) and isinstance(v, str)) for k, v in kwargs.items()]
+ ):
raise TypeError("Only string values are accepted for arguments")
for attribute_name, value in kwargs.items():