-
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.
Add import rename on
pydantic_settings
(#8)
- Loading branch information
Showing
6 changed files
with
113 additions
and
2 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,24 @@ | ||
from libcst.codemod import CodemodContext | ||
from libcst.codemod.commands.rename import RenameCommand | ||
|
||
|
||
def UsePydanticSettingsCommand(context: CodemodContext): | ||
"""Support for pydantic.BaseSettings. | ||
This command will rename pydantic.BaseSettings to pydantic_settings:BaseSettings. | ||
It doesn't support the following cases: | ||
- from pydantic.settings import BaseSettings | ||
- import pydantic ... class Settings(pydantic.BaseSettings) | ||
- import pydantic as pd ... class Settings(pd.BaseSettings) | ||
TODO: Support the above cases. To implement the above, you'll need to go to each | ||
`ClassDef`, and see the bases. If there's a `pydantic.settings.BaseSettings` in the | ||
bases, then you'll need to use `RemoveImportsVisitor` and `AddImportsVisitor` from | ||
`libcst.codemod.visitors`. | ||
""" | ||
return RenameCommand( | ||
context=context, | ||
old_name="pydantic.BaseSettings", | ||
new_name="pydantic_settings:BaseSettings", | ||
) |
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
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,5 @@ | ||
from pydantic import BaseSettings | ||
|
||
|
||
class Settings(BaseSettings): | ||
a: int |
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
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,71 @@ | ||
import pytest | ||
from libcst.codemod import CodemodTest | ||
|
||
from bump_pydantic.commands.use_settings import UsePydanticSettingsCommand | ||
|
||
|
||
class TestUsePydanticSettingsCommand(CodemodTest): | ||
TRANSFORM = lambda _, context: UsePydanticSettingsCommand(context) | ||
|
||
def test_base_settings(self): | ||
before = """ | ||
from pydantic import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
after = """ | ||
from pydantic_settings import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
@pytest.mark.skip(reason="Not implemented yet") | ||
def test_base_settings_import(self): | ||
before = """ | ||
from pydantic.settings import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
after = """ | ||
from pydantic_settings import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
@pytest.mark.skip(reason="Not implemented yet") | ||
def test_base_settings_import_from(self): | ||
before = """ | ||
import pydantic | ||
class Settings(pydantic.BaseSettings): | ||
foo: str | ||
""" | ||
after = """ | ||
from pydantic_settings import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
@pytest.mark.skip(reason="Not implemented yet") | ||
def test_base_settings_import_from_alias(self): | ||
before = """ | ||
import pydantic as pd | ||
class Settings(pd.BaseSettings): | ||
foo: str | ||
""" | ||
after = """ | ||
from pydantic_settings import BaseSettings | ||
class Settings(BaseSettings): | ||
foo: str | ||
""" | ||
self.assertCodemod(before, after) |
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