Skip to content

Linting and Formatting #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Linting

on:
push:
branches:
- master
pull_request:

jobs:
lint-dotnet:
name: Lint .NET
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.x'

- name: Lint
run: dotnet format -v diag --verify-no-changes --report=format.json

- uses: actions/upload-artifact@v4
with:
name: format-report
path: format.json

lint-python:
name: Lint Python
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2

- name: Install Ruff
run: pip install ruff

- name: Check formatting
run: ruff format --check

- name: Check lints
run: ruff check
1 change: 1 addition & 0 deletions clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
"""

from pythonnet import load

load()
16 changes: 9 additions & 7 deletions demo/DynamicGrid.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import clr
import sys
if sys.platform.lower() not in ['cli','win32']:

if sys.platform.lower() not in ["cli", "win32"]:
print("only windows is supported for wpf")
clr.AddReference(r"wpf\PresentationFramework")
from System.IO import StreamReader
from System.Windows.Markup import XamlReader
from System.Threading import Thread, ThreadStart, ApartmentState
from System.Windows import Application, Window

from System.IO import StreamReader # noqa: E402
from System.Windows.Markup import XamlReader # noqa: E402
from System.Threading import Thread, ThreadStart, ApartmentState # noqa: E402
from System.Windows import Application, Window # noqa: E402


class MyWindow(Window):
def __init__(self):
stream = StreamReader("DynamicGrid.xaml")
window = XamlReader.Load(stream.BaseStream)
Application().Run(window)


if __name__ == '__main__':

if __name__ == "__main__":
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
Expand Down
14 changes: 7 additions & 7 deletions demo/helloform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import clr

clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WinForms
from System.Drawing import Size, Point
import System.Windows.Forms as WinForms # noqa
from System.Drawing import Size, Point # noqa


class HelloApp(WinForms.Form):
"""A simple hello world app that demonstrates the essentials of
winforms programming and event-based programming in Python."""
winforms programming and event-based programming in Python."""

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(self):

def button_Click(self, sender, args):
"""Button click event handler"""
print ("Click")
print("Click")
WinForms.MessageBox.Show("Please do not press this button again.")

def run(self):
Expand All @@ -53,11 +53,11 @@ def run(self):

def main():
form = HelloApp()
print ("form created")
print("form created")
app = WinForms.Application
print ("app referenced")
print("app referenced")
app.Run(form)


if __name__ == '__main__':
if __name__ == "__main__":
main()
9 changes: 5 additions & 4 deletions demo/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import clr

import System

clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WinForms
import System.Windows.Forms as WinForms # noqa

from System.Drawing import Color, Size, Point
from System.Drawing import Color # noqa


class Splitter(WinForms.Form):
"""A WinForms example transcribed to Python from the MSDN article:
'Creating a Multipane User Interface with Windows Forms'."""
'Creating a Multipane User Interface with Windows Forms'."""

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -90,5 +91,5 @@ def main():
app.Dispose()


if __name__ == '__main__':
if __name__ == "__main__":
main()
80 changes: 47 additions & 33 deletions demo/wordpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

import clr
import System

clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WinForms
import System.Windows.Forms as WinForms # noqa

from System.IO import File
from System.Text import Encoding
from System.Drawing import Color, Point, Size
from System.Threading import ApartmentState, Thread, ThreadStart
from System.IO import File # noqa
from System.Text import Encoding # noqa
from System.Threading import ApartmentState, Thread, ThreadStart # noqa


class Wordpad(WinForms.Form):
"""A simple example winforms application similar to wordpad."""

def __init__(self):
super().__init__()
self.filename = ''
self.filename = ""
self.word_wrap = True
self.doctype = 1
self.InitializeComponent()
Expand Down Expand Up @@ -101,9 +101,14 @@ def InitializeComponent(self):
self.fileMenu.Text = "&File"
self.fileMenu.Index = 0

items = (self.menuFileNew, self.menuFileOpen,
self.menuFileSave, self.menuFileSaveAs,
self.menuFileSep_1, self.menuFileExit)
items = (
self.menuFileNew,
self.menuFileOpen,
self.menuFileSave,
self.menuFileSaveAs,
self.menuFileSep_1,
self.menuFileExit,
)

self.fileMenu.MenuItems.AddRange(items)

Expand Down Expand Up @@ -150,10 +155,16 @@ def InitializeComponent(self):
self.editMenu.Text = "&Edit"
self.editMenu.Index = 1

items = (self.menuEditUndo, self.menuEditRedo,
self.menuEditSep_1, self.menuEditCut,
self.menuEditCopy, self.menuEditPaste,
self.menuEditSep_2, self.menuEditSelectAll)
items = (
self.menuEditUndo,
self.menuEditRedo,
self.menuEditSep_1,
self.menuEditCut,
self.menuEditCopy,
self.menuEditPaste,
self.menuEditSep_2,
self.menuEditSelectAll,
)

self.editMenu.MenuItems.AddRange(items)

Expand Down Expand Up @@ -215,8 +226,7 @@ def InitializeComponent(self):
self.openFileDialog.Filter = "Text documents|*.txt|RTF document|*.rtf"
self.openFileDialog.Title = "Open document"

self.saveFileDialog.Filter = "Text Documents|*.txt|" \
"Rich Text Format|*.rtf"
self.saveFileDialog.Filter = "Text Documents|*.txt|" "Rich Text Format|*.rtf"
self.saveFileDialog.Title = "Save document"
self.saveFileDialog.FileName = "Untitled"

Expand Down Expand Up @@ -245,7 +255,7 @@ def OnClickFileSave(self, sender, args):
self.SaveDocument()

def OnClickFileSaveAs(self, sender, args):
self.filename = ''
self.filename = ""
self.SaveDocument()

def OnClickFileExit(self, sender, args):
Expand Down Expand Up @@ -285,10 +295,10 @@ def OnClickHelpAbout(self, sender, args):

def NewDocument(self):
self.doctype = 1
self.richTextBox.Rtf = ''
self.richTextBox.Text = ''
self.Text = 'Python Wordpad - (New Document)'
self.filename = ''
self.richTextBox.Rtf = ""
self.richTextBox.Text = ""
self.Text = "Python Wordpad - (New Document)"
self.filename = ""

def OpenDocument(self):
if self.openFileDialog.ShowDialog() != WinForms.DialogResult.OK:
Expand All @@ -308,19 +318,19 @@ def OpenDocument(self):
temp = Encoding.ASCII.GetString(buff, 0, read)
data.append(temp)

data = ''.join(data)
data = "".join(data)
stream.Close()

filename = self.filename = filename.lower()

if filename.endswith('.rtf'):
if filename.endswith(".rtf"):
self.richTextBox.Rtf = data
self.doctype = 2
else:
self.richTextBox.Text = data
self.doctype = 1

self.Text = 'Python Wordpad - %s' % filename
self.Text = "Python Wordpad - %s" % filename
self.richTextBox.Select(0, 0)

def SaveDocument(self):
Expand All @@ -332,13 +342,13 @@ def SaveDocument(self):
filename = self.saveFileDialog.FileName

filename = self.filename = filename.lower()
self.Text = 'Python Wordpad - %s' % filename
self.Text = "Python Wordpad - %s" % filename

self.richTextBox.Select(0, 0)

stream = File.OpenWrite(filename)

if filename.endswith('.rtf'):
if filename.endswith(".rtf"):
data = self.richTextBox.Rtf
else:
data = self.richTextBox.Text
Expand All @@ -350,11 +360,14 @@ def SaveDocument(self):

def SaveChangesDialog(self):
if self.richTextBox.Modified:
if WinForms.MessageBox.Show(
"Save changes?", "Word Pad",
WinForms.MessageBoxButtons.OK |
WinForms.MessageBoxButtons.YesNo
) == WinForms.DialogResult.Yes:
if (
WinForms.MessageBox.Show(
"Save changes?",
"Word Pad",
WinForms.MessageBoxButtons.OK | WinForms.MessageBoxButtons.YesNo,
)
== WinForms.DialogResult.Yes
):
self.SaveDocument()
return 1
return 0
Expand Down Expand Up @@ -384,8 +397,9 @@ def InitializeComponent(self):
self.label1.Name = "label1"
self.label1.Size = System.Drawing.Size(296, 140)
self.label1.TabIndex = 2
self.label1.Text = "Python Wordpad - an example winforms " \
"application using Python.NET"
self.label1.Text = (
"Python Wordpad - an example winforms " "application using Python.NET"
)

self.AutoScaleBaseSize = System.Drawing.Size(5, 13)
self.ClientSize = System.Drawing.Size(300, 150)
Expand Down Expand Up @@ -418,5 +432,5 @@ def main():
thread.Join()


if __name__ == '__main__':
if __name__ == "__main__":
main()
6 changes: 4 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os, sys
sys.path.insert(0, os.path.abspath('../..'))
import os
import sys

sys.path.insert(0, os.path.abspath("../.."))


# -- Project information -----------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pythonnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def load(runtime: Union[clr_loader.Runtime, str, None] = None, **params: str) ->

if func(b"") != 0:
raise RuntimeError("Failed to initialize Python.Runtime.dll")

_LOADED = True

import atexit
Expand Down
1 change: 1 addition & 0 deletions src/console/pythonconsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;

using Python.Runtime;

namespace Python.Runtime
Expand Down
4 changes: 3 additions & 1 deletion src/embed_tests/CodecGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Python.EmbeddingTest
{
using System;
using System.Linq;

using NUnit.Framework;

using Python.Runtime;
using Python.Runtime.Codecs;

Expand All @@ -20,7 +22,7 @@ public void GetEncodersByType()
};

var got = group.GetEncoders(typeof(Uri)).ToArray();
CollectionAssert.AreEqual(new[]{encoder1, encoder2}, got);
CollectionAssert.AreEqual(new[] { encoder1, encoder2 }, got);
}

[Test]
Expand Down
Loading
Loading