-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0db7e85
Showing
9 changed files
with
654 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023-present Marcelo Trylesinski <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# bump-pydantic | ||
|
||
[](https://pypi.org/project/bump-pydantic) | ||
[](https://pypi.org/project/bump-pydantic) | ||
|
||
----- | ||
|
||
**Table of Contents** | ||
|
||
- [bump-pydantic](#bump-pydantic) | ||
- [Installation](#installation) | ||
- [Checklist](#checklist) | ||
- [License](#license) | ||
|
||
## Installation | ||
|
||
```console | ||
pip install bump-pydantic | ||
``` | ||
|
||
## Checklist | ||
|
||
- [X] `Config` class to `model_config` attribute on `BaseModel`. | ||
- [ ] Removed Constrained<Type>" - raise warning. | ||
- [X] Replace imports that changed location. | ||
|
||
## License | ||
|
||
`bump-pydantic` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# SPDX-FileCopyrightText: 2023-present Marcelo Trylesinski <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
__version__ = "0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: 2023-present Marcelo Trylesinski <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import libcst as cst | ||
from libcst import matchers as m | ||
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand | ||
from libcst.codemod.visitors import AddImportsVisitor | ||
|
||
|
||
class BumpPydanticCodemodCommand(VisitorBasedCodemodCommand): | ||
DESCRIPTION = "Bump pydantic from 1.x to 2.x." | ||
|
||
def __init__(self, context: CodemodContext) -> None: | ||
super().__init__(context) | ||
self.config_args = [] | ||
|
||
@m.visit(m.ClassDef(name=m.Name(value="Config"))) | ||
def visit_config_class(self, node: cst.ClassDef) -> None: | ||
"""Collect the arguments from the `Config` class.""" | ||
for statement in node.body.body: | ||
if m.matches(statement, m.SimpleStatementLine()): | ||
statement = cst.ensure_type(statement, cst.SimpleStatementLine) | ||
for child in statement.body: | ||
if m.matches(child, m.Assign()): | ||
assignment = cst.ensure_type(child, cst.Assign) | ||
assign_target = cst.ensure_type( | ||
assignment.targets[0], cst.AssignTarget | ||
) | ||
keyword = cst.ensure_type(assign_target.target, cst.Name) | ||
keyword = keyword.with_changes( | ||
value=replace_config_attribute(keyword.value) | ||
) | ||
arg = cst.Arg( | ||
value=assignment.value, | ||
keyword=keyword, | ||
equal=cst.AssignEqual( | ||
whitespace_before=cst.SimpleWhitespace(""), | ||
whitespace_after=cst.SimpleWhitespace(""), | ||
), | ||
) | ||
self.config_args.append(arg) | ||
|
||
@m.leave( | ||
m.ClassDef( | ||
bases=[ | ||
m.ZeroOrMore(), | ||
m.Arg(), | ||
m.ZeroOrMore(), | ||
], | ||
body=m.IndentedBlock( | ||
body=[ | ||
m.ZeroOrMore(), | ||
m.ClassDef(name=m.Name(value="Config")), | ||
m.ZeroOrMore(), | ||
] | ||
), | ||
) | ||
) | ||
def leave_config_class( | ||
self, original_node: cst.ClassDef, updated_node: cst.ClassDef | ||
) -> cst.ClassDef: | ||
"""Replace the `Config` class with a `model_config` attribute. | ||
Any class that contains a `Config` class will have that class replaced | ||
with a `model_config` attribute. The `model_config` attribute will be | ||
assigned a `ConfigDict` object with the same arguments as the attributes | ||
from `Config` class. | ||
""" | ||
AddImportsVisitor.add_needed_import( | ||
context=self.context, module="pydantic", obj="ConfigDict" | ||
) | ||
block = cst.ensure_type(original_node.body, cst.IndentedBlock) | ||
body = [ | ||
statement | ||
if not m.matches(statement, m.ClassDef(name=m.Name(value="Config"))) | ||
else cst.SimpleStatementLine( | ||
body=[ | ||
cst.Assign( | ||
targets=[cst.AssignTarget(target=cst.Name("model_config"))], | ||
value=cst.Call( | ||
func=cst.Name("ConfigDict"), | ||
args=self.config_args, | ||
), | ||
) | ||
], | ||
) | ||
for statement in block.body | ||
] | ||
self.config_args = [] | ||
return updated_node.with_changes(body=updated_node.body.with_changes(body=body)) | ||
|
||
@m.leave( | ||
m.ImportFrom( | ||
module=m.Attribute( | ||
value=m.Name(value="pydantic"), | ||
attr=m.Name(value=m.MatchIfTrue(lambda value: value in MODULE_CHANGES)), | ||
), | ||
) | ||
) | ||
def replace_full_imports( | ||
self, _: cst.ImportFrom, updated_node: cst.ImportFrom | ||
) -> cst.ImportFrom: | ||
module = cst.ensure_type(updated_node.module, cst.Attribute) | ||
return updated_node.with_changes( | ||
module=module.with_changes( | ||
value=cst.Attribute( | ||
value=cst.Name("pydantic"), attr=cst.Name("deprecated") | ||
), | ||
attr=cst.Name(value=module.attr.value), | ||
) | ||
) | ||
|
||
@m.leave( | ||
m.ImportFrom( | ||
module=m.Name("pydantic"), | ||
names=[ | ||
m.ImportAlias( | ||
name=m.Name( | ||
value=m.MatchIfTrue(lambda value: value in MODULE_CHANGES) | ||
) | ||
) | ||
], | ||
) | ||
) | ||
def replace_partial_imports( | ||
self, _: cst.ImportFrom, updated_node: cst.ImportFrom | ||
) -> cst.ImportFrom: | ||
return updated_node.with_changes( | ||
module=cst.Attribute( | ||
value=cst.Name("pydantic"), attr=cst.Name("deprecated") | ||
) | ||
) | ||
|
||
|
||
CONFIG_ATTRS_REPLACEMENT = { | ||
"allow_population_by_field_name": "populate_by_name", | ||
"anystr_lower": "str_to_lower", | ||
"anystr_strip_whitespace": "str_strip_whitespace", | ||
"anystr_upper": "str_to_upper", | ||
"keep_untouched": "ignored_types", | ||
"max_anystr_length": "str_max_length", | ||
"min_anystr_length": "str_min_length", | ||
"orm_mode": "from_attributes", | ||
"validate_all": "validate_default", | ||
} | ||
|
||
|
||
def replace_config_attribute(old_name: str) -> str: | ||
return CONFIG_ATTRS_REPLACEMENT.get(old_name, old_name) | ||
|
||
|
||
MODULE_CHANGES = {"tools", "json", "parse", "decorator"} | ||
MODULE_CHANGES = {"tools", "json", "parse", "decorator"} |
Oops, something went wrong.