Skip to content

Commit 04673ff

Browse files
authored
refactor: Python 3.11 support, update URL map field (#433)
2 parents e311976 + c828db4 commit 04673ff

14 files changed

Lines changed: 89 additions & 24 deletions

File tree

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
python-version: ["3.10"]
13+
python-version: ["3.11"]
1414
steps:
1515
- uses: actions/checkout@v3
1616
- name: Filter changed file paths to outputs

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-latest
1717
strategy:
1818
matrix:
19-
python-version: [ '3.9', '3.10' ]
19+
python-version: [ '3.9', '3.11' ]
2020
steps:
2121
- uses: actions/checkout@v3
2222
- name: Install poetry
@@ -59,7 +59,7 @@ jobs:
5959

6060
strategy:
6161
matrix:
62-
python-version: ["3.10"]
62+
python-version: ["3.11"]
6363

6464
steps:
6565
- uses: actions/checkout@v3

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.10.7 3.9.13
1+
3.11.0 3.10.7 3.9.13

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
poetry 1.2.1
2-
python 3.10.7 3.9.13
2+
python 3.11.0 3.10.7 3.9.13

CHANGES

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ $ pip install --user --upgrade --pre libvcs
1515

1616
<!-- Maintainers, insert changes / features for the next release here -->
1717

18+
### What's new
19+
20+
#### Python 3.11 support (#433)
21+
22+
Official support for python 3.11
23+
24+
#### URLs: Mapping now class attributes (#433)
25+
26+
`URL.rule_map` is now a class attribute rather than a dataclass attribute.
27+
28+
```console
29+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 1211, in wrap
30+
return _process_class(cls, init, repr, eq, order, unsafe_hash,
31+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 959, in _process_class
33+
cls_fields.append(_get_field(cls, name, type, kw_only))
34+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 816, in _get_field
36+
raise ValueError(f'mutable default {type(f.default)} for field '
37+
ValueError: mutable default <class 'libvcs.url.base.RuleMap'> for field rule_map is not allowed: use default_factory
38+
```
39+
1840
## libvcs 0.19.1 (2022-10-23)
1941

2042
#### Tests
@@ -23,8 +45,7 @@ $ pip install --user --upgrade --pre libvcs
2345

2446
#### Documentation
2547

26-
- CLI, git: Split subcommands into separate pages (remote, stash, submodule),
27-
via #432
48+
- CLI, git: Split subcommands into separate pages (remote, stash, submodule), via #432
2849

2950
## libvcs 0.19.0 (2022-10-23)
3051

MIGRATION

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,47 @@ _Notes on the upcoming release will be added here_
2424

2525
<!-- Maintainers, insert migration notes for the next release here -->
2626

27+
### URLs: Mapping now class attributes (#433)
28+
29+
`URL.rule_map` is now a class attribute rather than a dataclass attribute.
30+
31+
Before:
32+
33+
```python
34+
@dataclasses.dataclass(repr=False)
35+
class GitLabURL(GitURL):
36+
rule_map: RuleMap = RuleMap(
37+
_rule_map={'gitlab_prefix': GitLabPrefix}
38+
)
39+
```
40+
41+
In python 3.11, that raises an error:
42+
43+
```console
44+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 1211, in wrap
45+
return _process_class(cls, init, repr, eq, order, unsafe_hash,
46+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 959, in _process_class
48+
cls_fields.append(_get_field(cls, name, type, kw_only))
49+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
File "/home/user/.python/3.11.0/lib/python3.11/dataclasses.py", line 816, in _get_field
51+
raise ValueError(f'mutable default {type(f.default)} for field '
52+
ValueError: mutable default <class 'libvcs.url.base.RuleMap'> for field rule_map is not allowed: use default_factory
53+
```
54+
55+
After release:
56+
57+
```python
58+
>>> import dataclasses
59+
>>> from libvcs.url.base import RuleMap
60+
>>> from libvcs.url.git import GitURL, DEFAULT_RULES
61+
>>> @dataclasses.dataclass(repr=False)
62+
... class MyGitURL(GitURL):
63+
... rule_map = RuleMap(
64+
... _rule_map={'gitlab_prefix': DEFAULT_RULES}
65+
... )
66+
```
67+
2768
<!---
2869
# vim: set filetype=markdown:
2970
-->

docs/url/registry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Prefix for KDE infrastructure, `kde:group/repository`:
6060

6161
>>> @dataclasses.dataclass(repr=False)
6262
... class MyGitURLParser(GitURL):
63-
... rule_map: RuleMap = RuleMap(
63+
... rule_map = RuleMap(
6464
... _rule_map={
6565
... **GitURL.rule_map._rule_map,
6666
... 'github_prefix': GitHubPrefix,

src/libvcs/url/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def register(self, cls: Rule) -> None:
101101
102102
>>> @dataclasses.dataclass(repr=False)
103103
... class GitHubURL(GitURL):
104-
... rule_map: RuleMap = RuleMap(
104+
... rule_map = RuleMap(
105105
... _rule_map={'github_prefix': GitHubPrefix}
106106
... )
107107
@@ -151,7 +151,7 @@ def register(self, cls: Rule) -> None:
151151
152152
>>> @dataclasses.dataclass(repr=False)
153153
... class GitLabURL(GitURL):
154-
... rule_map: RuleMap = RuleMap(
154+
... rule_map = RuleMap(
155155
... _rule_map={'gitlab_prefix': GitLabPrefix}
156156
... )
157157
@@ -187,7 +187,7 @@ def register(self, cls: Rule) -> None:
187187
188188
>>> @dataclasses.dataclass(repr=False)
189189
... class GitURLWithPip(GitBaseURL):
190-
... rule_map: RuleMap = RuleMap(
190+
... rule_map = RuleMap(
191191
... _rule_map={m.label: m for m in [*DEFAULT_RULES, *PIP_DEFAULT_RULES]}
192192
... )
193193

src/libvcs/url/git.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,11 @@ class GitBaseURL(URLProtocol, SkipDefaultFieldsReprMixin):
262262
# Decoration
263263
suffix: Optional[str] = None
264264

265+
# Matched
265266
rule: Optional[str] = None
266-
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in DEFAULT_RULES})
267+
268+
# Settings
269+
rule_map = RuleMap(_rule_map={m.label: m for m in DEFAULT_RULES})
267270

268271
def __post_init__(self) -> None:
269272
url = self.url
@@ -382,7 +385,7 @@ class GitPipURL(GitBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
382385
# commit-ish (rev): tag, branch, ref
383386
rev: Optional[str] = None
384387

385-
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in PIP_DEFAULT_RULES})
388+
rule_map = RuleMap(_rule_map={m.label: m for m in PIP_DEFAULT_RULES})
386389

387390
def to_url(self) -> str:
388391
"""Exports a pip-compliant URL.
@@ -490,7 +493,7 @@ class GitURL(GitPipURL, GitBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
490493
- :meth:`GitBaseURL.to_url`
491494
"""
492495

493-
rule_map: RuleMap = RuleMap(
496+
rule_map = RuleMap(
494497
_rule_map={m.label: m for m in [*DEFAULT_RULES, *PIP_DEFAULT_RULES]}
495498
)
496499

src/libvcs/url/hg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class HgBaseURL(URLProtocol, SkipDefaultFieldsReprMixin):
204204
rule: Optional[str] = None
205205
# name of the :class:`Rule`
206206

207-
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in DEFAULT_RULES})
207+
rule_map = RuleMap(_rule_map={m.label: m for m in DEFAULT_RULES})
208208

209209
def __post_init__(self) -> None:
210210
url = self.url
@@ -342,7 +342,7 @@ class HgPipURL(HgBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
342342
# commit-ish (rev): tag, branch, ref
343343
rev: Optional[str] = None
344344

345-
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in PIP_DEFAULT_RULES})
345+
rule_map = RuleMap(_rule_map={m.label: m for m in PIP_DEFAULT_RULES})
346346

347347
@classmethod
348348
def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
@@ -424,7 +424,7 @@ class HgURL(HgPipURL, HgBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
424424
- :meth:`HgBaseURL.to_url`
425425
"""
426426

427-
rule_map: RuleMap = RuleMap(
427+
rule_map = RuleMap(
428428
_rule_map={m.label: m for m in [*DEFAULT_RULES, *PIP_DEFAULT_RULES]}
429429
)
430430

0 commit comments

Comments
 (0)