Skip to content
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

Fix plenty of mypy misc+name-defined+attr-defined and pyright reportAttributeAccessIssue from pythoncom.py and pywintypes.py #2418

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ jobs:
strategy:
fail-fast: false
matrix:
# mypy won't understand "3.13-dev", keeping the CI simple by just omitting it
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand All @@ -150,7 +149,7 @@ jobs:
cache: pip
cache-dependency-path: .github/workflows/main.yml
check-latest: true
- run: pip install types-setuptools PyOpenGL mypy==1.11
- run: pip install types-pywin32 types-setuptools PyOpenGL mypy[faster-cache]==1.13.*
- run: mypy . --python-version=${{ matrix.python-version }}

pyright:
Expand All @@ -169,7 +168,7 @@ jobs:
cache-dependency-path: .github/workflows/main.yml
check-latest: true
# pyright vendors typeshed, but let's make sure we have the most up to date stubs
- run: pip install types-setuptools PyOpenGL
- run: pip install types-pywin32 types-setuptools PyOpenGL
- uses: jakebailey/pyright-action@v2
with:
python-version: ${{ matrix.python-version }}
Expand Down
9 changes: 9 additions & 0 deletions com/pythoncom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Magic utility that "redirects" to pythoncomXX.dll
from typing import TYPE_CHECKING, Any

import pywintypes

pywintypes.__import_pywin32_system_module__("pythoncom", globals())

# This module dynamically re-exports from a C-Extension.
# Prevent mypy attr-defined and pyright reportAttributeAccessIssue errors locally
# External usage should still prefer typeshed stubs
if TYPE_CHECKING:

def __getattr__(name: str) -> Any: ...
4 changes: 2 additions & 2 deletions com/win32com/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# flag if we are in a "frozen" build.
_frozen = getattr(sys, "frozen", False)
# pythoncom dumbly defaults this to zero - we believe sys.frozen over it.
if _frozen and not getattr(pythoncom, "frozen", 0):
pythoncom.frozen = sys.frozen
if _frozen and not getattr(pythoncom, "frozen", False):
pythoncom.frozen = _frozen

# Add support for an external "COM Extensions" path.
# Concept is that you can register a seperate path to be used for
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/server/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Note that we derive from com_error, which derives from builtin Exception
# Also note that we don't support "self.args", as we don't support tuple-unpacking
class COMException(pythoncom.com_error):
class COMException(pythoncom.com_error): # type: ignore[misc] # Because of mypy_path, mypy doesn't use typeshed's pythoncom here
"""An Exception object that is understood by the framework.

If the framework is presented with an exception of type class,
Expand Down
4 changes: 2 additions & 2 deletions com/win32comext/adsi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def QueryInterface(self, iid):


# We override the adsi.pyd methods to do the right thing.
def ADsGetObject(path, iid=pythoncom.IID_IDispatch):
def ADsGetObject(path, iid=pythoncom.IID_IDispatch): # type: ignore[no-redef]
ret = adsi.ADsGetObject(path, iid)
return _get_good_ret(ret)


def ADsOpenObject(path, username, password, reserved=0, iid=pythoncom.IID_IDispatch):
def ADsOpenObject(path, username, password, reserved=0, iid=pythoncom.IID_IDispatch): # type: ignore[no-redef]
ret = adsi.ADsOpenObject(path, username, password, reserved, iid)
return _get_good_ret(ret)
13 changes: 1 addition & 12 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ disallow_incomplete_defs = false
disable_error_code =
; Module has no attribute; (Dynamic modules will be hard to type without first-party stubs, ie: pythoncom.*)
attr-defined,
; Class cannot subclass "..." (has type "Any"); (IDEM)
; TODO: Use typeshed's types-pywin32 stubs after a few more fixes there
misc,
; Name "..." is not defined; (IDEM, leave undefined/unbound to Flake8/Ruff/pyright)
name-defined,
; Cannot assign to a method (we do lots of monkey patching)
method-assign,
exclude = (?x)(
Expand All @@ -47,13 +42,7 @@ exclude = (?x)(
[mypy-adsi.*,dde,exchange,mapi,perfmon,servicemanager,win32api,win32console,win32clipboard,win32comext.adsi.adsi,win32event,win32evtlog,win32file,win32gui,win32help,win32pdh,win32process,win32ras,win32security,win32service,win32trace,win32ui,win32uiole,win32wnet,_win32sysloader,_winxptheme]
ignore_missing_imports = True

; Most of win32com re-exports win32comext
; Test is a local untyped module in win32comext.axdebug
; pywin32_system32 is an empty module created in setup.py to store dlls
[mypy-win32com.*,Test,pywin32_system32]
ignore_missing_imports = True

; Distutils being removed from stdlib currently causes some issues on Python 3.12
; https://github.com/mhammond/pywin32/issues/2119
[mypy-distutils.*]
[mypy-Test,pywin32_system32]
ignore_missing_imports = True
6 changes: 2 additions & 4 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
"reportOptionalIterable": "warning",
"reportOptionalMemberAccess": "warning",
"reportOptionalSubscript": "warning",
// Needs fixes in types-pywin32 and requires Python 3.8 to annotate ambiguous global variables
"reportUnnecessaryComparison": "warning",
// TODO: Leave Unbound its own PR(s)
"reportUnboundVariable": "warning",
// Too many dynamically generated modules. This will require type stubs to properly fix.
Expand All @@ -49,8 +47,8 @@
// Since we're a library, and not user code, we care less about forgetting to install a dependency,
// as long as we have its stubs. So just disabling for now is fine.
"reportMissingModuleSource": "none",
// External type stubs may not be completable, and this will require type stubs for dynamic modules.
// External type stubs may not be completable
"reportMissingTypeStubs": "information",
// Use Flake8/Pycln/Ruff instead
// Use Pycln/Ruff instead
"reportUnusedImport": "none",
}
3 changes: 2 additions & 1 deletion win32/Lib/netbios.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def ACTION_HEADER():
ncb.Reset()
ncb.Command = NCBASTAT
ncb.Lana_num = la_enum.lana[i]
ncb.Callname = b"* "
# False-positive in mypy? Typeshed clearly types the setter as accepting bytes
ncb.Callname = b"* " # type: ignore[assignment]
adapter = ADAPTER_STATUS()
ncb.Buffer = adapter
Netbios(ncb)
Expand Down
8 changes: 8 additions & 0 deletions win32/Lib/pywintypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib.util
import os
import sys
from typing import TYPE_CHECKING, Any


def __import_pywin32_system_module__(modname, globs):
Expand Down Expand Up @@ -122,3 +123,10 @@ def __import_pywin32_system_module__(modname, globs):


__import_pywin32_system_module__("pywintypes", globals())

# This module dynamically re-exports from a C-Extension.
# Prevent mypy attr-defined and pyright reportAttributeAccessIssue errors locally
# External usage should still prefer typeshed stubs
if TYPE_CHECKING:

def __getattr__(name: str) -> Any: ...
Loading