Skip to content

expobrain/python-codemods

Repository files navigation

Unit testing, formatting & linting

Python codemods

This is a collection of codemods for Python packages basedon on LibCST.

Installation

Codemods are based on LibCST, it will be installed by running:

poetry install

Run the codemods

To run the codemods a small shell script mod is provided for convenience:

./mod wxpython.ColorToColourCommand [<source_code_path>, ...]

Run the tests

Tests are executed using Pytest which will be installed as dev requirements:

make test

Mypy's useful codemods

List of mods useful for migrating to Mypy.

DefaultFunctionReturnTypeCommand

For each function without a return type adds a default None return type; useful when during the introduction of Mypy a lot of functions are missing the return type and adding it by hand is impratical or inconvenient.

Example of convertion:

def fn f():
    pass

into:

def f() -> None:
    pass

wxPython 2.x to 4.x migrations

These are codemods available to migrate from wxPython 2.8 to 4.x

ColorToColourCommand

Converts calls to wx.Color into wx.Colour, i.e.:

wx.Color(255, 255, 255)

into:

wx.Colour(255, 255, 255)

ConstantsRenameCommand

Renames constants not available anymore into their equivalent in wxPython 4.x, i.e.:

wx.WXK_PRIOR
wx.WXK_NUMPAD_NEXT
wx.FILE_MUST_EXIST

into:

wx.WXK_PAGEUP
wx.WXK_NUMPAD_PAGEDOWN
wx.FD_FILE_MUST_EXIST

FixImportFromAdvCommand

Fix imports of symbols moved into the wx.adv package, i.e.:

import wx

wx.DatePickerCtrl(...)

into:

import wx.adv

wx.adv.DatePickerCtrl(...)

FlexGridSizerCommand

wx.FlexgridSizer needs now at least 3 arguments and this codemod add the third one with the default value, i.e.:

wx.FlexGridSizer(1, 0)

into:

wx.FlexGridSizer(1, 0, 0)

MenuAppendCommand

Renames keywords, like help into helpString, and renames the wx.Menu.AppendItem method into wx.Menu.Append if called with a single argument, i.e.:

menu.AppendItem(menu_item)
menu.Append(
    help="",
    id=1,
    kind=wx.ITEM_NORMAL,
    text="Menu item",
)

into :

menu.Append(menu_item)
menu.Append(
    helpString="",
    id=1,
    kind=wx.ITEM_NORMAL,
    item="Menu item",
)

ToolbarAddToolCommand

Renames calls to wx.Toolbar.DoAddTool into wx.Toolbar.AddTool, i.e.:

toolbar.DoAddTool(
    bitmap=my_bitmap,
    id=1,
    label="Toolbar tool"
)

into:

toolbar.AddTool(
    bitmap=my_bitmap,
    toolId=1,
    label="Toolbar tool"
)

SizerAddCommand

Renames calls to wx.Sizer.AddWindow into wx.Sizer.Add, i.e.:

sizer.AddWindow(panel, 0, border=0, flag=wx.EXPAND)

into:

sizer.Add(panel, 0, border=0, flag=wx.EXPAND)

ListCtrlInsertColumnCommand

Renames calls to wx.ListCtrl.InsertColumnInfo into wx.ListCtrl.InsertColumn, i.e.:

list_ctrl.InsertColumnInfo(0, info)

into:

list_ctrl.InsertColumn(0, info)

DeprecationWarningsCommand

Replaces all the deprecated symbols and methods with the eqivalent in wxPython 4.x, i.e.:

wx.BitmapFromImage()
wx.DateTimeFromDMY(*args)

into :

wx.Bitmap()
wx.DateTime.FromDMY(*args)

MakeModalCommand

The wx.Frame.MakeModal method has been removed and wxPython 4.x suggest to use a modal dialog instead, however for previous code to continue to work this codemod adds an artigicial MakeModal method to any class which calls self.MakeModal() in the code, i.e.:

class MyModal(wx.Frame):
    def __init__(self):
        super().__init__()

        self.MakeModal()

into:

class MyModal(wx.Frame):
    def __init__(self):
        super().__init__()

        self.MakeModal()

    def MakeModal(self, modal=True):
        if modal and not hasattr(self, '_disabler'):
            self._disabler = wx.WindowDisabler(self)
        if not modal and hasattr(self, '_disabler'):
            del self._disabler

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published