Skip to content

Commit c8a4c84

Browse files
Updated code formatting and lint (latest ruff, mypy) (#91)
1 parent 7de2c20 commit c8a4c84

40 files changed

+806
-520
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ repos:
1010
- id: end-of-file-fixer
1111
- id: requirements-txt-fixer
1212
- id: trailing-whitespace
13-
- repo: https://github.com/charliermarsh/ruff-pre-commit
14-
rev: 'v0.0.245'
13+
- repo: https://github.com/astral-sh/ruff-pre-commit
14+
rev: v0.3.3
1515
hooks:
1616
- id: ruff
17-
- repo: https://github.com/psf/black
18-
rev: 22.8.0
19-
hooks:
20-
- id: black
17+
- id: ruff-format

flake8_config

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

python_package/.pre-commit-config.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ repos:
1010
- id: end-of-file-fixer
1111
- id: requirements-txt-fixer
1212
- id: trailing-whitespace
13-
- repo: https://github.com/charliermarsh/ruff-pre-commit
14-
rev: 'v0.0.245'
13+
- repo: https://github.com/astral-sh/ruff-pre-commit
14+
rev: v0.3.3
1515
hooks:
1616
- id: ruff
17-
- repo: https://github.com/psf/black
18-
rev: 22.8.0
19-
hooks:
20-
- id: black
17+
- id: ruff-format

python_package/examples/basic_example.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,50 @@ async def main():
1616
},
1717
)
1818
print(await db.select("person"))
19-
print(await db.update("person", {
20-
"user":"you",
21-
"pass":"very_safe",
22-
"marketing": False,
23-
"tags": ["Awesome"]
24-
}))
19+
print(
20+
await db.update(
21+
"person",
22+
{
23+
"user": "you",
24+
"pass": "very_safe",
25+
"marketing": False,
26+
"tags": ["Awesome"],
27+
},
28+
)
29+
)
2530
print(await db.delete("person"))
2631

27-
# You can also use the query method
32+
# You can also use the query method
2833
# doing all of the above and more in SurrealQl
29-
30-
# In SurrealQL you can do a direct insert
34+
35+
# In SurrealQL you can do a direct insert
3136
# and the table will be created if it doesn't exist
32-
await db.query("""
33-
insert into person {
34-
user: 'me',
35-
pass: 'very_safe',
36-
tags: ['python', 'documentation']
37-
};
38-
39-
""")
37+
await db.query(
38+
"""
39+
insert into person {
40+
user: 'me',
41+
pass: 'very_safe',
42+
tags: ['python', 'documentation']
43+
};
44+
"""
45+
)
4046
print(await db.query("select * from person"))
41-
42-
print(await db.query("""
43-
update person content {
44-
user: 'you',
45-
pass: 'more_safe',
46-
tags: ['awesome']
47-
};
48-
49-
"""))
47+
48+
print(
49+
await db.query(
50+
"""
51+
update person content {
52+
user: 'you',
53+
pass: 'more_safe',
54+
tags: ['awesome']
55+
};
56+
"""
57+
)
58+
)
5059
print(await db.query("delete person"))
5160

61+
5262
if __name__ == "__main__":
5363
import asyncio
5464

55-
asyncio.run(main())
65+
asyncio.run(main())

python_package/pyproject.toml

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,52 @@ pydantic = "^1.10.6"
2828
websockets = "^10.4"
2929

3030
[tool.poetry.dev-dependencies]
31-
pre-commit = ">=2.20.0"
32-
black = ">=22.8.0"
33-
ruff = ">=0.0.245"
34-
mypy = ">=1.2.0"
31+
pre-commit = "3.6.2"
32+
ruff = "0.3.3"
33+
mypy = "1.9.0"
3534

3635
[build-system]
3736
requires = ["poetry-core>=1.0.0"]
3837
build-backend = "poetry.core.masonry.api"
3938

40-
[tool.black]
41-
line-length = 88
42-
color = true
43-
44-
exclude = '''
45-
/(
46-
\.bzr
47-
| \.direnv
48-
| \.eggs
49-
| \.git
50-
| \.hg
51-
| \.mypy_cache
52-
| \.nox
53-
| \.pants\.d
54-
| \.ruff_cache
55-
| \.__pypackages__
56-
| \.svn
57-
| \.tox
58-
| \.venv
59-
| _build
60-
| buck-out
61-
| build
62-
| dist
63-
| env
64-
| venv
65-
)/
66-
'''
67-
6839
[tool.ruff]
69-
select = ["I", "D", "N", "UP"]
40+
target-version = "py38"
7041
line-length = 88
71-
72-
ignore = [
42+
lint.select = [
43+
"B", # flake8-bugbear
44+
"C4", # flake8-comprehensions
45+
"D", # flake8-docstrings
46+
"E", # pycodestyle
47+
"EM", # flake8-errmsg
48+
"F", # pyflakes
49+
"FA", # flake8-future-annotations
50+
"FBT001", # flake8-boolean-trap
51+
"I", # isort
52+
"N", # pep8-naming
53+
"PIE", # flake8-pie
54+
"PT", # flake8-pytest-style
55+
"PTH", # flake8-use-pathlib
56+
"RUF", # Ruff-specific rules
57+
"SIM", # flake8-simplify
58+
"TCH", # flake8-type-checking
59+
"TD", # flake8-todos
60+
"TID", # flake8-tidy-imports
61+
"TRY", # tryceratops
62+
"UP", # pyupgrade
63+
"W", # pycodestyle
64+
]
65+
lint.ignore = [
7366
"D100", # Missing docstring in public module
7467
"D104", # Missing docstring in public package
7568
"D107", # Missing docstring in __init__
7669
"D205", # 1 blank line required between summary line and description
7770
"D212", # Multi-line docstring summary should start at the first line
71+
"E501", # Line too long (eg: docstring)
7872
"N805", # First argument of a method should be named self
7973
"N818", # Exception name ... should be named with an Error suffix
80-
"UP035" # Typing deprecations
74+
"TD002", # Missing author in TODO
75+
"TD003", # Missing issue link on the line following this TODO
8176
]
82-
8377
exclude = [
8478
".bzr",
8579
".direnv",
@@ -93,23 +87,28 @@ exclude = [
9387
".svn",
9488
".tox",
9589
".venv",
90+
"__pycache__",
9691
"__pypackages__",
9792
"_build",
9893
"buck-out",
9994
"build",
10095
"dist",
10196
"node_modules",
10297
"venv",
103-
".git",
104-
"__pycache__",
10598
]
10699

107-
[tool.ruff.pydocstyle]
100+
[tool.ruff.lint.per-file-ignores]
101+
"__init__.py" = ["F401"]
102+
103+
[tool.ruff.lint.pydocstyle]
108104
convention = "google"
109105

110-
[tool.ruff.pyupgrade]
106+
[tool.ruff.lint.pyupgrade]
111107
keep-runtime-typing = true
112108

109+
[tool.ruff.format]
110+
docstring-code-format = true
111+
113112
[tool.mypy]
114113
python_version = 3.8
115114
pretty = true

python_package/surrealdb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
1617
from .http import SurrealHTTP
1718
from .ws import Surreal
1819

python_package/surrealdb/http.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
1617
from __future__ import annotations
1718

1819
import json
1920
from dataclasses import dataclass
20-
from types import TracebackType
21-
from typing import Any, Dict, List, Optional, Type
21+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type
2222

2323
import httpx
2424

25+
if TYPE_CHECKING:
26+
from types import TracebackType
27+
2528
__all__ = ("SurrealHTTP",)
2629

2730

@@ -31,7 +34,8 @@ class SurrealException(Exception):
3134

3235
@dataclass(frozen=True)
3336
class SurrealResponse:
34-
"""Represents a http response from a SurrealDB server.
37+
"""
38+
Represents a http response from a SurrealDB server.
3539
3640
Attributes:
3741
time: The time the request was processed.
@@ -49,7 +53,8 @@ class SurrealResponse:
4953

5054

5155
class SurrealHTTP:
52-
"""Represents a http connection to a SurrealDB server.
56+
"""
57+
Represents a http connection to a SurrealDB server.
5358
5459
Args:
5560
url: The URL of the SurrealDB server.
@@ -125,18 +130,19 @@ async def _request(
125130
surreal_data = await surreal_response.aread()
126131
return json.loads(surreal_data)
127132

128-
# TODO add missing methods - currently undocumented
133+
# TODO: add missing methods - currently undocumented
129134
# Missing method - wait
130135
# Missing method - use
131136
# Missing method - invalidate
132137
# Missing method - authenticate
133138
# Missing method - let
134139
# Missing method - merge
135-
# TODO fix signup and signin methods
140+
# TODO: fix signup and signin methods
136141
# TODO: Review type: ignore comments.
137142

138143
async def signup(self, vars: Dict[str, Any]) -> str:
139-
"""Sign this connection up to a specific authentication scope.
144+
"""
145+
Sign this connection up to a specific authentication scope.
140146
141147
Args:
142148
vars: Variables used in a signup query.
@@ -150,7 +156,8 @@ async def signup(self, vars: Dict[str, Any]) -> str:
150156
return response # type: ignore
151157

152158
async def signin(self, vars: Dict[str, Any]) -> str:
153-
"""Sign this connection in to a specific authentication scope.
159+
"""
160+
Sign this connection in to a specific authentication scope.
154161
155162
Args:
156163
vars: Variables used in a signin query.
@@ -166,7 +173,8 @@ async def signin(self, vars: Dict[str, Any]) -> str:
166173
async def query(
167174
self, sql: str, vars: Optional[Dict[str, Any]] = None
168175
) -> List[Dict[str, Any]]:
169-
"""Run a set of SurrealQL statements against the database.
176+
"""
177+
Run a set of SurrealQL statements against the database.
170178
171179
Args:
172180
sql: Specifies the SurrealQL statements.
@@ -189,7 +197,8 @@ async def query(
189197
return response # type: ignore
190198

191199
async def select(self, thing: str) -> List[Dict[str, Any]]:
192-
"""Select all records in a table (or other entity),
200+
"""
201+
Select all records in a table (or other entity),
193202
or a specific record, in the database.
194203
195204
This function will run the following query in the database:
@@ -214,11 +223,13 @@ async def select(self, thing: str) -> List[Dict[str, Any]]:
214223
uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
215224
)
216225
if not response and record_id is not None:
217-
raise SurrealException(f"Key {record_id} not found in table {table}")
226+
msg = f"Key {record_id} not found in table {table}"
227+
raise SurrealException(msg)
218228
return response[0]["result"] # type: ignore
219229

220230
async def create(self, thing: str, data: Optional[Dict[str, Any]] = None) -> str:
221-
"""Create a record in the database.
231+
"""
232+
Create a record in the database.
222233
223234
This function will run the following query in the database:
224235
create $thing content $data
@@ -247,11 +258,13 @@ async def create(self, thing: str, data: Optional[Dict[str, Any]] = None) -> str
247258
data=json.dumps(data, ensure_ascii=False),
248259
)
249260
if not response and record_id is not None:
250-
raise SurrealException(f"Key {record_id} not found in table {table}")
261+
msg = f"Key {record_id} not found in table {table}"
262+
raise SurrealException(msg)
251263
return response[0]["result"] # type: ignore
252264

253265
async def update(self, thing: str, data: Any) -> Dict[str, Any]:
254-
"""Update all records in a table, or a specific record, in the database.
266+
"""
267+
Update all records in a table, or a specific record, in the database.
255268
256269
This function replaces the current document / record data with the
257270
specified data.
@@ -285,7 +298,8 @@ async def update(self, thing: str, data: Any) -> Dict[str, Any]:
285298
return response[0]["result"] # type: ignore
286299

287300
async def patch(self, thing: str, data: Any) -> Dict[str, Any]:
288-
"""Apply JSON Patch changes to all records, or a specific record, in the database.
301+
"""
302+
Apply JSON Patch changes to all records, or a specific record, in the database.
289303
290304
This function patches the current document / record data with
291305
the specified JSON Patch data.
@@ -318,7 +332,8 @@ async def patch(self, thing: str, data: Any) -> Dict[str, Any]:
318332
return response[0]["result"] # type: ignore
319333

320334
async def delete(self, thing: str) -> List[Dict[str, Any]]:
321-
"""Delete all records in a table, or a specific record, from the database.
335+
"""
336+
Delete all records in a table, or a specific record, from the database.
322337
323338
This function will run the following query in the database:
324339
delete * from $thing

0 commit comments

Comments
 (0)