by Guest » 03 Jan 2025, 18:14
Ich würde gerne eine einfache Prüfung durchführen, ob eine Variable für den Boto3-Client leer ist. Ich habe den folgenden Ansatz versucht:
Code: Select all
"""Example of boto3 client lazy initialization"""
from typing import Optional
import boto3
from botocore.client import BaseClient
from mypy_boto3_ec2 import EC2Client
class ClassA:
"""Abstract class which lazily initializes the boto3 client"""
def __init__(self) -> None:
self._client: Optional[BaseClient] = None
self.client_type: str = ""
@property
def client(self):
"""Lazy boto3 client initialization"""
if not self._client:
self._client = boto3.client(self.client_type)
return self._client
class ClassB(ClassA):
"""One of many concrete child classes of the ClassA"""
def __init__(self) -> None:
super().__init__()
self._client: Optional[EC2Client] = None
self.client_type: str = "ec2"
def some_method(self) -> dict:
"""Just a method to try random EC2Client functionality"""
result = self.client.describe_instances()
return result
Aber dieser Code hat viele Tippprobleme (eine Zusammenfassung der von mypy und pyright gefundenen Probleme):
- Ich kann nicht Überschreiben Sie den übergeordneten Typ BaseClient durch die konkrete Klasse EC2Client
- Wenn ich das Optional für den Clienttyp weglasse, ist der Wert None nicht vorhanden kompatibel mit den Typen BaseClient und EC2Client
- Wenn ich „client“ optional lasse, hat NoneType kein Attribut „describe_instances“.
- Wenn ich „ClassA._client“ untypisiert und gleich „None“ lasse, ist es wieder die untergeordnete Klasse „client“. inkompatibel.
Wie gebe ich also eine leere Variable für einen boto3-Client ein? Wie hoch sollte der Wert sein, damit ich erkenne, dass die Variable leer ist? (Wie mache ich es optional, ohne das NoneType-Attributproblem?)
Fragen, die ich als Quellen verwendet habe:
- Ich versuche, rund um boto3 Annotationen einzugeben, aber das Modul „botocore.client“ hat kein Attribut „EC2“.
- Was ist die richtige Typannotation für boto3.client(service)
Variablenuntertyp Override ist inkompatibel
Ich würde gerne eine einfache Prüfung durchführen, ob eine Variable für den Boto3-Client leer ist. Ich habe den folgenden Ansatz versucht:
[code]"""Example of boto3 client lazy initialization"""
from typing import Optional
import boto3
from botocore.client import BaseClient
from mypy_boto3_ec2 import EC2Client
class ClassA:
"""Abstract class which lazily initializes the boto3 client"""
def __init__(self) -> None:
self._client: Optional[BaseClient] = None
self.client_type: str = ""
@property
def client(self):
"""Lazy boto3 client initialization"""
if not self._client:
self._client = boto3.client(self.client_type)
return self._client
class ClassB(ClassA):
"""One of many concrete child classes of the ClassA"""
def __init__(self) -> None:
super().__init__()
self._client: Optional[EC2Client] = None
self.client_type: str = "ec2"
def some_method(self) -> dict:
"""Just a method to try random EC2Client functionality"""
result = self.client.describe_instances()
return result
[/code]
Aber dieser Code hat viele Tippprobleme (eine Zusammenfassung der von mypy und pyright gefundenen Probleme):
[list]
[*]Ich kann nicht Überschreiben Sie den übergeordneten Typ BaseClient durch die konkrete Klasse EC2Client
[*]Wenn ich das Optional für den Clienttyp weglasse, ist der Wert None nicht vorhanden kompatibel mit den Typen BaseClient und EC2Client
[*]Wenn ich „client“ optional lasse, hat NoneType kein Attribut „describe_instances“.
[*]Wenn ich „ClassA._client“ untypisiert und gleich „None“ lasse, ist es wieder die untergeordnete Klasse „client“. inkompatibel.
[/list]
Wie gebe ich also eine leere Variable für einen boto3-Client ein? Wie hoch sollte der Wert sein, damit ich erkenne, dass die Variable leer ist? (Wie mache ich es optional, ohne das NoneType-Attributproblem?)
Fragen, die ich als Quellen verwendet habe:
[list]
[*] Ich versuche, rund um boto3 Annotationen einzugeben, aber das Modul „botocore.client“ hat kein Attribut „EC2“.
[*]Was ist die richtige Typannotation für boto3.client(service)
Variablenuntertyp Override ist inkompatibel
[/list]