From: Michael R. Crusoe <crusoe@debian.org>
Subject: avoid an unnecessary mypy_boto3_s3 import
Forwarded: https://github.com/DataBiosphere/toil/pull/4763
--- toil.orig/src/toil/jobStores/aws/utils.py
+++ toil/src/toil/jobStores/aws/utils.py
@@ -17,13 +17,12 @@
 import os
 import types
 from ssl import SSLError
-from typing import Optional, cast
+from typing import Optional, cast, TYPE_CHECKING
 
 from boto3.s3.transfer import TransferConfig
 from boto.exception import SDBResponseError
 from botocore.client import Config
 from botocore.exceptions import ClientError
-from mypy_boto3_s3 import S3Client, S3ServiceResource
 
 from toil.lib.aws import session
 from toil.lib.aws.utils import connection_reset, get_bucket_region
@@ -36,6 +35,8 @@
                             get_error_status,
                             old_retry,
                             retry)
+if TYPE_CHECKING:
+    from mypy_boto3_s3 import S3Client, S3ServiceResource
 
 logger = logging.getLogger(__name__)
 
@@ -290,7 +291,7 @@
     error=ClientError,
     error_codes=[404, 500, 502, 503, 504]
 )])
-def copyKeyMultipart(resource: S3ServiceResource,
+def copyKeyMultipart(resource: "S3ServiceResource",
                      srcBucketName: str,
                      srcKeyName: str,
                      srcKeyVersion: str,
@@ -346,7 +347,7 @@
     # not wherever the bucket virtual hostnames go.
     source_region = get_bucket_region(srcBucketName)
     source_client = cast(
-        S3Client,
+        "S3Client",
         session.client(
             's3',
             region_name=source_region,
--- toil.orig/src/toil/test/jobStores/jobStoreTest.py
+++ toil/src/toil/test/jobStores/jobStoreTest.py
@@ -40,7 +40,6 @@
 from toil.jobStores.abstractJobStore import (NoSuchFileException,
                                              NoSuchJobException)
 from toil.jobStores.fileJobStore import FileJobStore
-from toil.lib.aws.utils import create_s3_bucket, get_object_for_url
 from toil.lib.io import mkdtemp
 from toil.lib.memoize import memoize
 from toil.lib.retry import retry
@@ -1463,6 +1462,7 @@
 
     def _hashTestFile(self, url: str) -> str:
         from toil.jobStores.aws.jobStore import AWSJobStore
+        from toil.lib.aws.utils import get_object_for_url
         str(AWSJobStore)  # to prevent removal of that import
         key = get_object_for_url(urlparse.urlparse(url), existing=True)
         contents = key.get().get('Body').read()
@@ -1471,7 +1471,7 @@
     def _createExternalStore(self):
         """A S3.Bucket instance is returned"""
         from toil.jobStores.aws.jobStore import establish_boto3_session
-        from toil.lib.aws.utils import retry_s3
+        from toil.lib.aws.utils import retry_s3, create_s3_bucket
 
         resource = establish_boto3_session().resource(
             "s3", region_name=self.awsRegion()
--- toil.orig/src/toil/test/lib/test_ec2.py
+++ toil/src/toil/test/lib/test_ec2.py
@@ -20,7 +20,6 @@
                               feed_flatcar_ami_release,
                               flatcar_release_feed_amis,
                               get_flatcar_ami)
-from toil.lib.aws.session import establish_boto3_session
 from toil.test import ToilTest, needs_aws_ec2, needs_online
 
 logger = logging.getLogger(__name__)
@@ -59,6 +58,7 @@
 class AMITest(ToilTest):
     @classmethod
     def setUpClass(cls):
+        from toil.lib.aws.session import establish_boto3_session
         session = establish_boto3_session(region_name='us-west-2')
         cls.ec2_client = session.client('ec2')
 
