youtype / mypy_boto3_builder

Type annotations builder for boto3 compatible with VSCode, PyCharm, Emacs, Sublime Text, pyright and mypy.
https://youtype.github.io/mypy_boto3_builder/
MIT License
553 stars 38 forks source link

Define a type alias for service names #107

Closed Gaasmann closed 3 years ago

Gaasmann commented 3 years ago

Hello,

I'm encountering a problem and I wonder if there is a way of fixing it.

I have that kind of code:

import boto3
from botocore.client import BaseClient

def create_boto_client(service: str) -> BaseClient:
    return boto3.client(service)

mypy is not happy as service is an str but boto3.client expects a name from all the Literal["something"] defined for each service name.

The code under boto3-stubs.init defines an "@override def client" for each service. I was wondering if it could also be possible to generate a type alias like ServiceName = Literal["ec2", "s3", "sqs", ...] and so on that I could use instead of str in my function.

Thanks,

-- Nicolas

vemel commented 3 years ago

Makes sense. However, boto3-stubs itself is a stubs-only package and cannot be imported directly.

I can, however, add this Literal to all service packages, so it can be imported as any other Literal type.

vemel commented 3 years ago

I added ServiceName literal to all service literals, e.g. from mypy_boto3_ec2.literals import ServiceName.

The change is included in today's boto3-stubs 1.18.43 release. Let me know if it works as expected!

Gaasmann commented 3 years ago

Thank you, I'm able to import it and PyCharm seems happy with that now. Unfortunately, it seems that mypy isn't. I'm not sure if it is a mypy issue. Let me know if you think of anything that could explain it. Otherwise, I'll open an issue on the mypy project.

The mypy error:

test.py:7: error: Argument 1 to "client" has incompatible type "Union[Literal['accessanalyzer'], Literal['acm'], Literal['acm-pca'], Literal['alexaforbusiness'], Literal['amp'], Literal['amplify'], Literal['amplifybackend'], Literal['apigateway'], Literal['apigatewaymanagementapi'], Literal['apigatewayv2'], Literal['appconfig'], Literal['appflow'], Literal['appintegrations'], Literal['application-autoscaling'], Literal['application-insights'], Literal['applicationcostprofiler'], Literal['appmesh'], Literal['apprunner'], Literal['appstream'], Literal['appsync'], Literal['athena'], Literal['auditmanager'], Literal['autoscaling'], Literal['autoscaling-plans'], Literal['backup'], Literal['batch'], Literal['braket'], Literal['budgets'], Literal['ce'], Literal['chime'], Literal['chime-sdk-identity'], Literal['chime-sdk-messaging'], Literal['cloud9'], Literal['clouddirectory'], Literal['cloudformation'], Literal['cloudfront'], Literal['cloudhsm'], Literal['cloudhsmv2'], Literal['cloudsearch'], Literal['cloudsearchdomain'], Literal['cloudtrail'], Literal['cloudwatch'], Literal['codeartifact'], Literal['codebuild'], Literal['codecommit'], Literal['codedeploy'], Literal['codeguru-reviewer'], Literal['codeguruprofiler'], Literal['codepipeline'], Literal['codestar'], Literal['codestar-connections'], Literal['codestar-notifications'], Literal['cognito-identity'], Literal['cognito-idp'], Literal['cognito-sync'], Literal['comprehend'], Literal['comprehendmedical'], Literal['compute-optimizer'], Literal['config'], Literal['connect'], Literal['connect-contact-lens'], Literal['connectparticipant'], Literal['cur'], Literal['customer-profiles'], Literal['databrew'], Literal['dataexchange'], Literal['datapipeline'], Literal['datasync'], Literal['dax'], Literal['detective'], Literal['devicefarm'], Literal['devops-guru'], Literal['directconnect'], Literal['discovery'], Literal['dlm'], Literal['dms'], Literal['docdb'], Literal['ds'], Literal['dynamodb'], Literal['dynamodbstreams'], Literal['ebs'], Literal['ec2'], Literal['ec2-instance-connect'], Literal['ecr'], Literal['ecr-public'], Literal['ecs'], Literal['efs'], Literal['eks'], Literal['elastic-inference'], Literal['elasticache'], Literal['elasticbeanstalk'], Literal['elastictranscoder'], Literal['elb'], Literal['elbv2'], Literal['emr'], Literal['emr-containers'], Literal['es'], Literal['events'], Literal['finspace'], Literal['finspace-data'], Literal['firehose'], Literal['fis'], Literal['fms'], Literal['forecast'], Literal['forecastquery'], Literal['frauddetector'], Literal['fsx'], Literal['gamelift'], Literal['glacier'], Literal['globalaccelerator'], Literal['glue'], Literal['greengrass'], Literal['greengrassv2'], Literal['groundstation'], Literal['guardduty'], Literal['health'], Literal['healthlake'], Literal['honeycode'], Literal['iam'], Literal['identitystore'], Literal['imagebuilder'], Literal['importexport'], Literal['inspector'], Literal['iot'], Literal['iot-data'], Literal['iot-jobs-data'], Literal['iot1click-devices'], Literal['iot1click-projects'], Literal['iotanalytics'], Literal['iotdeviceadvisor'], Literal['iotevents'], Literal['iotevents-data'], Literal['iotfleethub'], Literal['iotsecuretunneling'], Literal['iotsitewise'], Literal['iotthingsgraph'], Literal['iotwireless'], Literal['ivs'], Literal['kafka'], Literal['kafkaconnect'], Literal['kendra'], Literal['kinesis'], Literal['kinesis-video-archived-media'], Literal['kinesis-video-media'], Literal['kinesis-video-signaling'], Literal['kinesisanalytics'], Literal['kinesisanalyticsv2'], Literal['kinesisvideo'], Literal['kms'], Literal['lakeformation'], Literal['lambda'], Literal['lex-models'], Literal['lex-runtime'], Literal['lexv2-models'], Literal['lexv2-runtime'], Literal['license-manager'], Literal['lightsail'], Literal['location'], Literal['logs'], Literal['lookoutequipment'], Literal['lookoutmetrics'], Literal['lookoutvision'], Literal['machinelearning'], Literal['macie'], Literal['macie2'], Literal['managedblockchain'], Literal['marketplace-catalog'], Literal['marketplace-entitlement'], Literal['marketplacecommerceanalytics'], Literal['mediaconnect'], Literal['mediaconvert'], Literal['medialive'], Literal['mediapackage'], Literal['mediapackage-vod'], Literal['mediastore'], Literal['mediastore-data'], Literal['mediatailor'], Literal['memorydb'], Literal['meteringmarketplace'], Literal['mgh'], Literal['mgn'], Literal['migrationhub-config'], Literal['mobile'], Literal['mq'], Literal['mturk'], Literal['mwaa'], Literal['neptune'], Literal['network-firewall'], Literal['networkmanager'], Literal['nimble'], Literal['opensearch'], Literal['opsworks'], Literal['opsworkscm'], Literal['organizations'], Literal['outposts'], Literal['personalize'], Literal['personalize-events'], Literal['personalize-runtime'], Literal['pi'], Literal['pinpoint'], Literal['pinpoint-email'], Literal['pinpoint-sms-voice'], Literal['polly'], Literal['pricing'], Literal['proton'], Literal['qldb'], Literal['qldb-session'], Literal['quicksight'], Literal['ram'], Literal['rds'], Literal['rds-data'], Literal['redshift'], Literal['redshift-data'], Literal['rekognition'], Literal['resource-groups'], Literal['resourcegroupstaggingapi'], Literal['robomaker'], Literal['route53'], Literal['route53-recovery-cluster'], Literal['route53-recovery-control-config'], Literal['route53-recovery-readiness'], Literal['route53domains'], Literal['route53resolver'], Literal['s3'], Literal['s3control'], Literal['s3outposts'], Literal['sagemaker'], Literal['sagemaker-a2i-runtime'], Literal['sagemaker-edge'], Literal['sagemaker-featurestore-runtime'], Literal['sagemaker-runtime'], Literal['savingsplans'], Literal['schemas'], Literal['sdb'], Literal['secretsmanager'], Literal['securityhub'], Literal['serverlessrepo'], Literal['service-quotas'], Literal['servicecatalog'], Literal['servicecatalog-appregistry'], Literal['servicediscovery'], Literal['ses'], Literal['sesv2'], Literal['shield'], Literal['signer'], Literal['sms'], Literal['sms-voice'], Literal['snow-device-management'], Literal['snowball'], Literal['sns'], Literal['sqs'], Literal['ssm'], Literal['ssm-contacts'], Literal['ssm-incidents'], Literal['sso'], Literal['sso-admin'], Literal['sso-oidc'], Literal['stepfunctions'], Literal['storagegateway'], Literal['sts'], Literal['support'], Literal['swf'], Literal['synthetics'], Literal['textract'], Literal['timestream-query'], Literal['timestream-write'], Literal['transcribe'], Literal['transfer'], Literal['translate'], Literal['waf'], Literal['waf-regional'], Literal['wafv2'], Literal['wellarchitected'], Literal['workdocs'], Literal['worklink'], Literal['workmail'], Literal['workmailmessageflow'], Literal['workspaces'], Literal['xray']]"; expected "Literal['accessanalyzer']"

From the following code:

import boto3
from botocore.client import BaseClient
from mypy_boto3_ec2.literals import ServiceName

def create_boto_client(service: ServiceName) -> BaseClient:
    return boto3.client(service)