Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b2677b

Browse files
committed
Migrate to ruff.
1 parentc61c97c commit5b2677b

17 files changed

+196
-112
lines changed

‎.bandit.yml‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎.flake8‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎.pre-commit-config.yaml‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
repos:
2-
-repo:https://github.com/PyCQA/bandit
3-
rev:1.7.10
2+
-repo:https://github.com/astral-sh/ruff-pre-commit
3+
rev:v0.9.3
44
hooks:
5-
-id:bandit
6-
args:[-r, -c, .bandit.yml]
7-
-repo:https://github.com/PyCQA/flake8
8-
rev:7.1.1
9-
hooks:
10-
-id:flake8
11-
-repo:https://github.com/psf/black.git
12-
rev:24.10.0
13-
hooks:
14-
-id:black
15-
-repo:https://github.com/pycqa/isort
16-
rev:5.13.2
17-
hooks:
18-
-id:isort
19-
-repo:https://github.com/asottile/pyupgrade
20-
rev:v3.19.1
21-
hooks:
22-
-id:pyupgrade
23-
args:[--py39-plus]
5+
-id:ruff
6+
args:[ --fix ]
7+
-id:ruff-format

‎docs/conf.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env python
22

3-
importos
43
importsys
4+
frompathlibimportPath
55

66
# Get the project root dir, which is the parent dir of this
7-
cwd=os.getcwd()
8-
project_root=os.path.dirname(cwd)
7+
project_root=str(Path.cwd().parent)
98

109
# Insert the project root dir as the first element in the PYTHONPATH.
1110
# This lets us ensure that the source package is imported, and that its

‎docs/conftest.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
importos
21
fromdoctestimportELLIPSIS,NORMALIZE_WHITESPACE
2+
frompathlibimportPath
33

44
fromsybilimportSybil
55

@@ -16,9 +16,8 @@
1616

1717

1818
defload_selector(filename,**kwargs):
19-
input_path=os.path.join(os.path.dirname(__file__),"_static",filename)
20-
withopen(input_path,encoding="utf-8")asinput_file:
21-
returnSelector(text=input_file.read(),**kwargs)
19+
input_path=Path(__file__).parent/"_static"/filename
20+
returnSelector(text=input_path.read_text(encoding="utf-8"),**kwargs)
2221

2322

2423
defsetup(namespace):

‎parsel/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"xpathfuncs",
1414
]
1515

16-
fromparselimportxpathfuncs# NOQA
17-
fromparsel.csstranslatorimportcss2xpath# NOQA
18-
fromparsel.selectorimportSelector,SelectorList# NOQA
16+
fromparselimportxpathfuncs
17+
fromparsel.csstranslatorimportcss2xpath
18+
fromparsel.selectorimportSelector,SelectorList
1919

2020
xpathfuncs.setup()

‎parsel/csstranslator.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616

1717
classXPathExpr(OriginalXPathExpr):
18-
1918
textnode:bool=False
2019
attribute:str|None=None
2120

@@ -143,5 +142,5 @@ def css_to_xpath(self, css: str, prefix: str = "descendant-or-self::") -> str:
143142

144143

145144
defcss2xpath(query:str)->str:
146-
"Return translated XPath version of a given CSS query"
145+
"""Return translated XPath version of a given CSS query"""
147146
return_translator.css_to_xpath(query)

‎parsel/selector.py‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
importjson
77
importtyping
88
importwarnings
9-
fromcollections.abcimportMapping
109
fromioimportBytesIO
11-
fromreimportPattern
12-
fromtypingimportAny,Literal,SupportsIndex,TypedDict,TypeVar,Union
10+
fromtypingimportTYPE_CHECKING,Any,Literal,SupportsIndex,TypedDict,TypeVar,Union
1311

1412
importjmespath
1513
fromlxmlimportetree,html
@@ -18,6 +16,11 @@
1816
from .csstranslatorimportGenericTranslator,HTMLTranslator
1917
from .utilsimportextract_regex,flatten,iflatten,shorten
2018

19+
ifTYPE_CHECKING:
20+
fromcollections.abcimportMapping
21+
fromreimportPattern
22+
23+
2124
_SelectorType=TypeVar("_SelectorType",bound="Selector")
2225
_ParserType=Union[etree.XMLParser,etree.HTMLParser]
2326
# simplified _OutputMethodArg from types-lxml
@@ -50,7 +53,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
5053

5154

5255
classCTGroupValue(TypedDict):
53-
_parser:type[etree.XMLParser]|type[html.HTMLParser]
56+
_parser:type[etree.XMLParser|html.HTMLParser]
5457
_csstranslator:GenericTranslator|HTMLTranslator
5558
_tostring_method:_TostringMethodType
5659

@@ -97,7 +100,8 @@ def create_root_node(
97100
if"use XML_PARSE_HUGE option"inerror.message:
98101
warnings.warn(
99102
f"Input data is too big. Upgrade to lxml "
100-
f"{lxml_huge_tree_version} or later for huge_tree support."
103+
f"{lxml_huge_tree_version} or later for huge_tree support.",
104+
stacklevel=2,
101105
)
102106
ifrootisNone:
103107
root=etree.fromstring(b"<html/>",parser=parser,base_url=base_url)
@@ -124,8 +128,7 @@ def __getitem__(
124128
o=super().__getitem__(pos)
125129
ifisinstance(pos,slice):
126130
returnself.__class__(typing.cast("SelectorList[_SelectorType]",o))
127-
else:
128-
returntyping.cast(_SelectorType,o)
131+
returntyping.cast(_SelectorType,o)
129132

130133
def__getstate__(self)->None:
131134
raiseTypeError("can't pickle SelectorList objects")
@@ -341,7 +344,7 @@ def _get_root_type(root: Any, *, input_type: str | None) -> str:
341344
f"and{input_type!r} as type."
342345
)
343346
return_xml_or_html(input_type)
344-
elifisinstance(root, (dict,list))or_is_valid_json(root):
347+
ifisinstance(root, (dict,list))or_is_valid_json(root):
345348
return"json"
346349
returninput_typeor"json"
347350

@@ -392,14 +395,14 @@ class Selector:
392395
"""
393396

394397
__slots__= [
395-
"namespaces",
396-
"type",
398+
"__weakref__",
397399
"_expr",
398400
"_huge_tree",
399-
"root",
400401
"_text",
401402
"body",
402-
"__weakref__",
403+
"namespaces",
404+
"root",
405+
"type",
403406
]
404407

405408
_default_namespaces= {
@@ -541,8 +544,7 @@ def jmespath(
541544
defmake_selector(x:Any)->_SelectorType:# closure function
542545
ifisinstance(x,str):
543546
returnself.__class__(text=x,_expr=query,type="text")
544-
else:
545-
returnself.__class__(root=x,_expr=query)
547+
returnself.__class__(root=x,_expr=query)
546548

547549
result= [make_selector(x)forxinresult]
548550
returntyping.cast(SelectorList[_SelectorType],self.selectorlist_cls(result))
@@ -707,10 +709,9 @@ def get(self) -> Any:
707709
except (AttributeError,TypeError):
708710
ifself.rootisTrue:
709711
return"1"
710-
elifself.rootisFalse:
712+
ifself.rootisFalse:
711713
return"0"
712-
else:
713-
returnstr(self.root)
714+
returnstr(self.root)
714715

715716
extract=get
716717

‎parsel/utils.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__importannotations
22

33
importre
4-
fromcollections.abcimportIterable,Iterator
5-
fromreimportMatch,Pattern
6-
fromtypingimportAny,cast
4+
fromtypingimportTYPE_CHECKING,Any,cast
75

86
fromw3lib.htmlimportreplace_entitiesasw3lib_replace_entities
97

8+
ifTYPE_CHECKING:
9+
fromcollections.abcimportIterable,Iterator
10+
1011

1112
defflatten(x:Iterable[Any])->list[Any]:
1213
"""flatten(sequence) -> list
@@ -61,7 +62,7 @@ def _is_listlike(x: Any) -> bool:
6162

6263

6364
defextract_regex(
64-
regex:str|Pattern[str],text:str,replace_entities:bool=True
65+
regex:str|re.Pattern[str],text:str,replace_entities:bool=True
6566
)->list[str]:
6667
"""Extract a list of strings from the given text/encoding using the following policies:
6768
* if the regex contains a named group called "extract" that will be returned
@@ -74,7 +75,7 @@ def extract_regex(
7475
if"extract"inregex.groupindex:
7576
# named group
7677
try:
77-
extracted=cast(Match[str],regex.search(text)).group("extract")
78+
extracted=cast(re.Match[str],regex.search(text)).group("extract")
7879
exceptAttributeError:
7980
strings= []
8081
else:

‎parsel/xpathfuncs.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,4 @@ def has_class(context: Any, *classes: str) -> bool:
5454
returnFalse
5555
node_cls=" "+node_cls+" "
5656
node_cls=replace_html5_whitespaces(" ",node_cls)
57-
forclsinclasses:
58-
if" "+cls+" "notinnode_cls:
59-
returnFalse
60-
returnTrue
57+
returnall(" "+cls+" "innode_clsforclsinclasses)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp