Skip to content

Commit c79952e

Browse files
authored
[SYNPY-1282] Adds Type Hinting to client.py (#987)
Adds type hints to several functions in `client.py` and a quick note to CONTRIBUTING.md because of an issue getting the `pipenv install` to work properly.
1 parent df6468b commit c79952e

2 files changed

Lines changed: 35 additions & 34 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Perform the following one-time steps to set up your local environment.
5959
1. This package uses Python, if you have not already, please install [pyenv](https://github.com/pyenv/pyenv#installation) to manage your Python versions. Versions supported by this package are all versions >=3.8 and <=3.11. If you do not install `pyenv` make sure that Python and `pip` are installed correctly and have been added to your PATH by running `python3 --version` and `pip3 --version`. If your installation was successful, your terminal will return the versions of Python and `pip` that you installed. **Note**: If you have `pyenv` it will install a specific version of Python for you.
6060
6161
2. Install `pipenv` by running `pip install pipenv`.
62+
- If you already have `pipenv` installed, ensure that the version is >=2023.9.8 to avoid compatibility issues.
6263
6364
3. Install `synapseclient` locally using pipenv:
6465

synapseclient/client.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,16 @@ class Synapse(object):
221221
# TODO: add additional boolean for write to disk?
222222
def __init__(
223223
self,
224-
repoEndpoint=None,
225-
authEndpoint=None,
226-
fileHandleEndpoint=None,
227-
portalEndpoint=None,
228-
debug=None,
229-
skip_checks=False,
230-
configPath=CONFIG_FILE,
231-
requests_session=None,
232-
cache_root_dir=None,
233-
silent=None,
224+
repoEndpoint: str = None,
225+
authEndpoint: str = None,
226+
fileHandleEndpoint: str = None,
227+
portalEndpoint: str = None,
228+
debug: bool = None,
229+
skip_checks: bool = False,
230+
configPath: str = CONFIG_FILE,
231+
requests_session: requests.Session = None,
232+
cache_root_dir: str = None,
233+
silent: bool = None,
234234
):
235235
self._requests_session = requests_session or requests.Session()
236236

@@ -296,20 +296,20 @@ def _init_logger(self):
296296
logging.getLogger("py.warnings").handlers = self.logger.handlers
297297

298298
@property
299-
def max_threads(self):
299+
def max_threads(self) -> int:
300300
return self._max_threads
301301

302302
@max_threads.setter
303303
def max_threads(self, value: int):
304304
self._max_threads = min(max(value, 1), MAX_THREADS_CAP)
305305

306306
@property
307-
def username(self):
307+
def username(self) -> Union[str, None]:
308308
# for backwards compatability when username was a part of the Synapse object and not in credentials
309309
return self.credentials.username if self.credentials is not None else None
310310

311311
@functools.lru_cache()
312-
def getConfigFile(self, configPath):
312+
def getConfigFile(self, configPath: str) -> configparser.RawConfigParser:
313313
"""
314314
Retrieves the client configuration information.
315315
@@ -328,11 +328,11 @@ def getConfigFile(self, configPath):
328328

329329
def setEndpoints(
330330
self,
331-
repoEndpoint=None,
332-
authEndpoint=None,
333-
fileHandleEndpoint=None,
334-
portalEndpoint=None,
335-
skip_checks=False,
331+
repoEndpoint: str = None,
332+
authEndpoint: str = None,
333+
fileHandleEndpoint: str = None,
334+
portalEndpoint: str = None,
335+
skip_checks: bool = False,
336336
):
337337
"""
338338
Sets the locations for each of the Synapse services (mostly useful for testing).
@@ -385,14 +385,14 @@ def setEndpoints(
385385

386386
def login(
387387
self,
388-
email=None,
389-
password=None,
390-
apiKey=None,
391-
sessionToken=None,
392-
rememberMe=False,
393-
silent=False,
394-
forced=False,
395-
authToken=None,
388+
email: str = None,
389+
password: str = None,
390+
apiKey: str = None,
391+
sessionToken: str = None,
392+
rememberMe: bool = False,
393+
silent: bool = False,
394+
forced: bool = False,
395+
authToken: str = None,
396396
):
397397
"""
398398
Valid combinations of login() arguments:
@@ -508,26 +508,26 @@ def login(
508508
)
509509
)
510510

511-
def _get_config_section_dict(self, section_name):
511+
def _get_config_section_dict(self, section_name: str) -> dict:
512512
config = self.getConfigFile(self.configPath)
513513
try:
514514
return dict(config.items(section_name))
515515
except configparser.NoSectionError:
516516
# section not present
517517
return {}
518518

519-
def _get_config_authentication(self):
519+
def _get_config_authentication(self) -> str:
520520
return self._get_config_section_dict(
521521
config_file_constants.AUTHENTICATION_SECTION_NAME
522522
)
523523

524-
def _get_client_authenticated_s3_profile(self, endpoint, bucket):
524+
def _get_client_authenticated_s3_profile(self, endpoint: str, bucket: str) -> str:
525525
config_section = endpoint + "/" + bucket
526526
return self._get_config_section_dict(config_section).get(
527527
"profile_name", "default"
528528
)
529529

530-
def _get_transfer_config(self):
530+
def _get_transfer_config(self) -> dict:
531531
# defaults
532532
transfer_config = {"max_threads": DEFAULT_NUM_THREADS, "use_boto_sts": False}
533533

@@ -552,7 +552,7 @@ def _get_transfer_config(self):
552552

553553
return transfer_config
554554

555-
def _getSessionToken(self, email, password):
555+
def _getSessionToken(self, email: str, password: str) -> str:
556556
"""Returns a validated session token."""
557557
try:
558558
req = {"email": email, "password": password}
@@ -572,7 +572,7 @@ def _getSessionToken(self, email, password):
572572
raise SynapseAuthenticationError("Invalid username or password.")
573573
raise
574574

575-
def _getAPIKey(self, sessionToken):
575+
def _getAPIKey(self, sessionToken: str) -> str:
576576
"""Uses a session token to fetch an API key."""
577577

578578
headers = {"sessionToken": sessionToken, "Accept": "application/json"}
@@ -591,7 +591,7 @@ def _is_logged_in(self) -> bool:
591591
return False
592592
return True
593593

594-
def logout(self, forgetMe=False):
594+
def logout(self, forgetMe: bool = False):
595595
"""
596596
Removes authentication information from the Synapse client.
597597
@@ -654,7 +654,7 @@ def getUserProfile(
654654
)
655655
)
656656

657-
def _findPrincipals(self, query_string):
657+
def _findPrincipals(self, query_string: str) -> typing.List[UserGroupHeader]:
658658
"""
659659
Find users or groups by name or email.
660660

0 commit comments

Comments
 (0)