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 1 commit
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
Prev Previous commit
Next Next commit
Format and fix lints in Python (test) code
  • Loading branch information
filmor committed Mar 31, 2024
commit 850265f781cd2e63e2c30f3f3c7280857c497a48
20 changes: 20 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ jobs:
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
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ license = {text = "MIT"}
readme = "README.rst"

dependencies = [
"clr_loader>=0.2.6,<0.3.0"
"clr-loader>=0.2.6",
]

requires-python = ">=3.7, <3.13"
requires-python = ">=3.8, <3.13"

classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: C#",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down Expand Up @@ -58,3 +57,9 @@ xfail_strict = true
testpaths = [
"tests"
]

[tool.pdm.dev-dependencies]
dev = [
"pytest>=8.1.1",
"find-libpython>=0.4.0",
]
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: 0 additions & 1 deletion tests/_missing_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-

import this_package_should_never_exist_ever
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def pytest_configure(config):
)

import os

os.environ["PYTHONNET_RUNTIME"] = runtime_opt
for k, v in runtime_params.items():
os.environ[f"PYTHONNET_{runtime_opt.upper()}_{k.upper()}"] = v
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy