Skip to content

Commit c27de26

Browse files
authored
Improve some type hints and fix a bug (#741)
Misc clean-up. Remove unused imports, change a var name to avoid conflict with build-in name, improve some (not all) type hints, fix a bug. Fixes #740
1 parent 8225fbb commit c27de26

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

kaggle/api/kaggle_api_extended.py

+28-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import urllib3.exceptions as urllib3_exceptions
3535
from requests import RequestException
3636

37-
from kaggle.models.kaggle_models_extended import ResumableUploadResult, File, Kernel
37+
from kaggle.models.kaggle_models_extended import ResumableUploadResult, File
3838

3939
from requests.adapters import HTTPAdapter
4040
from slugify import slugify
@@ -59,7 +59,6 @@
5959
ApiDatasetNewFile,
6060
ApiUpdateDatasetMetadataRequest,
6161
ApiGetDatasetMetadataRequest,
62-
ApiListDatasetFilesResponse,
6362
ApiDatasetFile,
6463
)
6564
from kagglesdk.datasets.types.dataset_enums import (
@@ -108,9 +107,9 @@
108107

109108
class DirectoryArchive(object):
110109

111-
def __init__(self, fullpath, format):
110+
def __init__(self, fullpath, fmt):
112111
self._fullpath = fullpath
113-
self._format = format
112+
self._format = fmt
114113
self.name = None
115114
self.path = None
116115

@@ -216,7 +215,7 @@ def _is_previous_valid(self, previous):
216215
and previous.timestamp > time.time() - ResumableFileUpload.RESUMABLE_UPLOAD_EXPIRY_SECONDS
217216
)
218217

219-
def upload_initiated(self, start_blob_upload_response):
218+
def upload_initiated(self, start_blob_upload_response: ApiStartBlobUploadRequest):
220219
if self.context.no_resume:
221220
return
222221

@@ -339,7 +338,7 @@ class KaggleApi:
339338
# Competitions valid types
340339
valid_competition_groups = ['general', 'entered', 'community', 'hosted', 'unlaunched', 'unlaunched_community']
341340
valid_competition_categories = [
342-
'all',
341+
'unspecified',
343342
'featured',
344343
'research',
345344
'recruitment',
@@ -460,7 +459,7 @@ def authenticate(self) -> None:
460459
# Step 3: load into configuration!
461460
self._load_config(config_data)
462461

463-
def _is_help_or_version_command(self, api_command):
462+
def _is_help_or_version_command(self, api_command: str):
464463
"""Determines if the string command passed in is for a help or version
465464
command.
466465
@@ -471,9 +470,7 @@ def _is_help_or_version_command(self, api_command):
471470
"""
472471
return api_command.endswith(('-h', '--help', '-v', '--version'))
473472

474-
def read_config_environment(
475-
self, config_data: Optional[Dict[Any, Any]] = None, quiet: bool = False
476-
) -> Dict[str, str]:
473+
def read_config_environment(self, config_data: Optional[Dict[str, str]] = None) -> Dict[str, str]:
477474
"""read_config_environment is the second effort to get a username and key
478475
to authenticate to the Kaggle API. The environment keys are equivalent to
479476
the kaggle.json file, but with "KAGGLE_" prefix to define a unique
@@ -482,7 +479,6 @@ def read_config_environment(
482479
Parameters
483480
==========
484481
config_data: a partially loaded configuration dictionary (optional)
485-
quiet: suppress verbose print of output (default is False)
486482
"""
487483

488484
# Add all variables that start with KAGGLE_ to config data
@@ -722,7 +718,7 @@ def camel_to_snake(self, name: str) -> str:
722718
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
723719
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
724720

725-
def lookup_enum(self, enum_class: Any, item_name: str) -> Enum:
721+
def lookup_enum(self, enum_class, item_name: str) -> Enum:
726722
item = self.camel_to_snake(item_name).upper()
727723
try:
728724
return enum_class[item]
@@ -739,7 +735,7 @@ def lookup_enum(self, enum_class: Any, item_name: str) -> Enum:
739735
return enum_class[item]
740736
raise
741737

742-
def short_enum_name(self, value):
738+
def short_enum_name(self, value: str):
743739
full_name = str(value)
744740
names = full_name.split('.')
745741
prefix_len = len(self.camel_to_snake(names[0])) + 1 # underscore
@@ -750,10 +746,10 @@ def short_enum_name(self, value):
750746
def competitions_list(
751747
self,
752748
group: Optional[str] = None,
753-
category: None = None,
754-
sort_by: None = None,
755-
page: int = 1,
756-
search: None = None,
749+
category: Optional[str] = None,
750+
sort_by: Optional[str] = None,
751+
page: Optional[int] = 1,
752+
search: Optional[str] = None,
757753
):
758754
"""Make a call to list competitions, format the response, and return a list
759755
of ApiCompetition instances.
@@ -777,9 +773,12 @@ def competitions_list(
777773

778774
if category:
779775
if category not in self.valid_competition_categories:
780-
raise ValueError(
781-
'Invalid category specified. Valid options are ' + str(self.valid_competition_categories)
782-
)
776+
if category == 'all':
777+
category = 'unspecified'
778+
else:
779+
raise ValueError(
780+
'Invalid category specified. Valid options are ' + str(self.valid_competition_categories)
781+
)
783782
category = self.lookup_enum(HostSegment, category)
784783

785784
if sort_by:
@@ -797,7 +796,15 @@ def competitions_list(
797796
response = kaggle.competitions.competition_api_client.list_competitions(request)
798797
return response.competitions
799798

800-
def competitions_list_cli(self, group=None, category=None, sort_by=None, page=1, search=None, csv_display=False):
799+
def competitions_list_cli(
800+
self,
801+
group: Optional[str] = None,
802+
category: Optional[str] = None,
803+
sort_by: Optional[str] = None,
804+
page: Optional[int] = 1,
805+
search: Optional[str] = None,
806+
csv_display: Optional[bool] = False,
807+
):
801808
"""A wrapper for competitions_list for the client.
802809
803810
Parameters

0 commit comments

Comments
 (0)