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

Start adding types using monkeytype. #1432

Merged
merged 1 commit into from
Jan 11, 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
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ jobs:
path: build/tox/compare.txt
- store_artifacts:
path: build/tox/compare.yaml
type:
executor: toxandnode
resource_class: large
steps:
- checkout
- tox:
env: type
wheels:
executor: toxandnode
steps:
Expand Down Expand Up @@ -333,6 +340,13 @@ workflows:
branches:
ignore:
- gh-pages
- type:
filters:
tags:
only: /^v.*/
branches:
ignore:
- gh-pages
- compare:
filters:
tags:
Expand All @@ -355,6 +369,7 @@ workflows:
- py311
- py312
- lint_and_docs
- type
- wheels
filters:
tags:
Expand All @@ -369,6 +384,7 @@ workflows:
- py311
- py312
- lint_and_docs
- type
- wheels
filters:
tags:
Expand All @@ -393,5 +409,6 @@ workflows:
- py311
- py312
- lint_and_docs
- type
- compare
- wheels
9 changes: 5 additions & 4 deletions .circleci/dcm4chee/upload_example_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def upload_example_data(server_url):

# This is TCGA-AA-3697
sha512s = [
'48cb562b94d0daf4060abd9eef150c851d3509d9abbff4bea11d00832955720bf1941073a51e6fb68fb5cc23704dec2659fc0c02360a8ac753dc523dca2c8c36', # noqa
'36432183380eb7d44417a2210a19d550527abd1181255e19ed5c1d17695d8bb8ca42f5b426a63fa73b84e0e17b770401a377ae0c705d0ed7fdf30d571ef60e2d', # noqa
'99bd3da4b8e11ce7b4f7ed8a294ed0c37437320667a06c40c383f4b29be85fe8e6094043e0600bee0ba879f2401de4c57285800a4a23da2caf2eb94e5b847ee0', # noqa
'48cb562b94d0daf4060abd9eef150c851d3509d9abbff4bea11d00832955720bf1941073a51e6fb68fb5cc23704dec2659fc0c02360a8ac753dc523dca2c8c36', # noqa
'36432183380eb7d44417a2210a19d550527abd1181255e19ed5c1d17695d8bb8ca42f5b426a63fa73b84e0e17b770401a377ae0c705d0ed7fdf30d571ef60e2d', # noqa
'99bd3da4b8e11ce7b4f7ed8a294ed0c37437320667a06c40c383f4b29be85fe8e6094043e0600bee0ba879f2401de4c57285800a4a23da2caf2eb94e5b847ee0', # noqa
]
download_urls = [
f'https://data.kitware.com/api/v1/file/hashsum/sha512/{x}/download' for x in sha512s
Expand All @@ -35,6 +35,7 @@ def upload_example_data(server_url):

url = os.getenv('DICOMWEB_TEST_URL')
if url is None:
raise Exception('DICOMWEB_TEST_URL must be set')
msg = 'DICOMWEB_TEST_URL must be set'
raise Exception(msg)

upload_example_data(url)
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Optimizing when reading arrays rather than images from tiff files ([#1423](../../pull/1423))
- Better filter DICOM adjacent files to ensure they share series instance IDs ([#1424](../../pull/1424))
- Optimizing small getRegion calls and some tiff tile fetches ([#1427](../../pull/1427)
- Started adding python types to the core library ([#1430](../../pull/1430)

### Changed
- Cleanup some places where get was needlessly used ([#1428](../../pull/1428)
Expand Down
10 changes: 6 additions & 4 deletions large_image/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
import re
from typing import cast
from typing import Any, Optional, Union, cast

from . import exceptions

Expand Down Expand Up @@ -59,7 +59,8 @@
}


def getConfig(key=None, default=None):
def getConfig(key: Optional[str] = None,
default: Optional[Union[str, bool, int, logging.Logger]] = None) -> Any:
"""
Get the config dictionary or a value from the cache config settings.

Expand All @@ -83,7 +84,8 @@ def getConfig(key=None, default=None):
return ConfigValues.get(key, default)


def getLogger(key=None, default=None):
def getLogger(key: Optional[str] = None,
default: Optional[logging.Logger] = None) -> logging.Logger:
"""
Get a logger from the config. Ensure that it is a valid logger.

Expand All @@ -97,7 +99,7 @@ def getLogger(key=None, default=None):
return logger


def setConfig(key, value):
def setConfig(key: str, value: Optional[Union[str, bool, int, logging.Logger]]) -> None:
"""
Set a value in the config settings.

Expand Down
2 changes: 1 addition & 1 deletion large_image/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TileSourceInefficientError(TileSourceError):


class TileSourceFileNotFoundError(TileSourceError, FileNotFoundError):
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
return super().__init__(errno.ENOENT, *args, **kwargs)


Expand Down
35 changes: 24 additions & 11 deletions large_image/tilesource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
import uuid
from importlib.metadata import entry_points
from pathlib import PosixPath
from typing import Dict, List, Optional, Tuple, Type, Union

from .. import config
from ..constants import NEW_IMAGE_PATH_FLAG, SourcePriority
Expand All @@ -13,10 +15,10 @@
FileTileSource, TileOutputMimeTypes, TileSource,
dictToEtree, etreeToDict, nearPowerOfTwo)

AvailableTileSources = {}
AvailableTileSources: Dict[str, Type[FileTileSource]] = {}


def isGeospatial(path):
def isGeospatial(path: Union[str, PosixPath]) -> bool:
"""
Check if a path is likely to be a geospatial file.

Expand All @@ -38,7 +40,8 @@ def isGeospatial(path):
return False


def loadTileSources(entryPointName='large_image.source', sourceDict=AvailableTileSources):
def loadTileSources(entryPointName: str = 'large_image.source',
sourceDict: Dict[str, Type[FileTileSource]] = AvailableTileSources) -> None:
"""
Load all tilesources from entrypoints and add them to the
AvailableTileSources dictionary.
Expand All @@ -61,7 +64,10 @@ def loadTileSources(entryPointName='large_image.source', sourceDict=AvailableTil
'Failed to loaded tile source %s' % entryPoint.name)


def getSortedSourceList(availableSources, pathOrUri, mimeType=None, *args, **kwargs):
def getSortedSourceList(
availableSources: Dict[str, Type[FileTileSource]], pathOrUri: Union[str, PosixPath],
mimeType: Optional[str] = None, *args, **kwargs,
) -> List[Tuple[bool, bool, SourcePriority, str]]:
"""
Get an ordered list of sources where earlier sources are more likely to
work for a specified path or uri.
Expand Down Expand Up @@ -108,7 +114,9 @@ def getSortedSourceList(availableSources, pathOrUri, mimeType=None, *args, **kwa
return sourceList


def getSourceNameFromDict(availableSources, pathOrUri, mimeType=None, *args, **kwargs):
def getSourceNameFromDict(
availableSources: Dict[str, Type[FileTileSource]], pathOrUri: Union[str, PosixPath],
mimeType: Optional[str] = None, *args, **kwargs) -> Optional[str]:
"""
Get a tile source based on a ordered dictionary of known sources and a path
name or URI. Additional parameters are passed to the tile source and can
Expand All @@ -125,9 +133,12 @@ def getSourceNameFromDict(availableSources, pathOrUri, mimeType=None, *args, **k
for _clash, _fallback, _priority, sourceName in sorted(sourceList):
if availableSources[sourceName].canRead(pathOrUri, *args, **kwargs):
return sourceName
return None


def getTileSourceFromDict(availableSources, pathOrUri, *args, **kwargs):
def getTileSourceFromDict(
availableSources: Dict[str, Type[FileTileSource]], pathOrUri: Union[str, PosixPath],
*args, **kwargs) -> FileTileSource:
"""
Get a tile source based on a ordered dictionary of known sources and a path
name or URI. Additional parameters are passed to the tile source and can
Expand All @@ -146,7 +157,7 @@ def getTileSourceFromDict(availableSources, pathOrUri, *args, **kwargs):
raise TileSourceError('No available tilesource for %s' % pathOrUri)


def getTileSource(*args, **kwargs):
def getTileSource(*args, **kwargs) -> FileTileSource:
"""
Get a tilesource using the known sources. If tile sources have not yet
been loaded, load them.
Expand All @@ -158,7 +169,7 @@ def getTileSource(*args, **kwargs):
return getTileSourceFromDict(AvailableTileSources, *args, **kwargs)


def open(*args, **kwargs):
def open(*args, **kwargs) -> FileTileSource:
"""
Alternate name of getTileSource.

Expand All @@ -170,7 +181,7 @@ def open(*args, **kwargs):
return getTileSource(*args, **kwargs)


def canRead(*args, **kwargs):
def canRead(*args, **kwargs) -> bool:
"""
Check if large_image can read a path or uri.

Expand All @@ -187,7 +198,9 @@ def canRead(*args, **kwargs):
return False


def canReadList(pathOrUri, mimeType=None, *args, **kwargs):
def canReadList(
pathOrUri: Union[str, PosixPath], mimeType: Optional[str] = None,
*args, **kwargs) -> List[Tuple[str, bool]]:
"""
Check if large_image can read a path or uri via each source.

Expand All @@ -210,7 +223,7 @@ def canReadList(pathOrUri, mimeType=None, *args, **kwargs):
return result


def new(*args, **kwargs):
def new(*args, **kwargs) -> TileSource:
"""
Create a new image.

Expand Down
Loading