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 static typing issue & turn on mypy+pyright in adodbapi #2279

Merged
merged 1 commit into from
Jun 30, 2024
Merged
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
5 changes: 2 additions & 3 deletions adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,8 @@ def __setattr__(self, name, value):
f"paramstyle={value!r} not in:{api.accepted_paramstyles!r}",
)
elif name == "variantConversions":
value = copy.copy(
value
) # make a new copy -- no changes in the default, please
# make a new copy -- no changes in the default, please
value = copy.copy(value)
object.__setattr__(self, name, value)

def __getattr__(self, item):
Expand Down
25 changes: 17 additions & 8 deletions adodbapi/apibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
* http://sourceforge.net/projects/adodbapi
"""

from __future__ import annotations

import datetime
import decimal
import numbers
import sys
import time
from collections.abc import Callable, Iterable, Mapping
from typing import Dict

# noinspection PyUnresolvedReferences
from . import ado_consts as adc
Expand Down Expand Up @@ -461,20 +465,25 @@ def convert_to_python(variant, func): # convert DB value into Python value
return func(variant) # call the appropriate conversion function


class MultiMap(dict): # builds a dictionary from {(sequence,of,keys) : function}
"""A dictionary of ado.type : function -- but you can set multiple items by passing a sequence of keys"""
class MultiMap(Dict[int, Callable[[object], object]]):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typing issue here was that dict leads to partially Any type. Since this is a very small and simple class, I completely typed it.

# builds a dictionary from {(iterable,of,keys) : function}
"""A dictionary of ado.type : function
-- but you can set multiple items by passing an iterable of keys"""

# useful for defining conversion functions for groups of similar data types.
def __init__(self, aDict):
for k, v in list(aDict.items()):
def __init__(self, aDict: Mapping[Iterable[int] | int, Callable[[object], object]]):
for k, v in aDict.items():
Copy link
Collaborator Author

@Avasam Avasam Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItemsView (or in the case of a dict, dict_items) is already iterable. coercing to a list was redundant.

self[k] = v # we must call __setitem__

def __setitem__(self, adoType, cvtFn):
"set a single item, or a whole sequence of items"
try: # user passed us a sequence, set them individually
def __setitem__(
self, adoType: Iterable[int] | int, cvtFn: Callable[[object], object]
):
"set a single item, or a whole iterable of items"
if isinstance(adoType, Iterable):
# user passed us an iterable, set them individually
for type in adoType:
dict.__setitem__(self, type, cvtFn)
except TypeError: # a single value fails attempt to iterate
else:
dict.__setitem__(self, adoType, cvtFn)


Expand Down
28 changes: 13 additions & 15 deletions adodbapi/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,6 @@
Adodbapi can be run on CPython 3.5 and later.
"""

CLASSIFIERS = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Operating System :: Microsoft :: Windows
Operating System :: POSIX :: Linux
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: SQL
Topic :: Software Development
Topic :: Software Development :: Libraries :: Python Modules
Topic :: Database
"""

NAME = "adodbapi"
MAINTAINER = "Vernon Cole"
MAINTAINER_EMAIL = "[email protected]"
Expand All @@ -25,7 +11,19 @@
)
URL = "http://sourceforge.net/projects/adodbapi"
LICENSE = "LGPL"
CLASSIFIERS = filter(None, CLASSIFIERS.split("\n"))
CLASSIFIERS = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: SQL",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Database",
]
AUTHOR = "Henrik Ekelund, Vernon Cole, et.al."
AUTHOR_EMAIL = "[email protected]"
PLATFORMS = ["Windows", "Linux"]
Expand Down
2 changes: 0 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ exclude = (?x)(
| ^Pythonwin/Scintilla/
; Forked IDLE extensions predating Python 2.3. They now live in idlelib in https://github.com/python/cpython/tree/main/Lib/idlelib
| ^Pythonwin/pywin/idle/
; TODO: adodbapi should be updated and fixed separately
| ^adodbapi/
; TODO: Ignoring non-public APIs until all public API is typed
| ([Tt]est|[Dd]emos?)/
)
Expand Down
2 changes: 0 additions & 2 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"**/test/",
"**/Demos/",
"**/demo/",
// TODO: adodbapi should be updated and fixed separately
"adodbapi/",
],
// Packages that will be accessible globally.
// Setting this makes pyright use the repo's code for those modules instead of typeshed or pywin32 in site-packages
Expand Down
Loading