VS Code 및 PyCharm과 같은 IDE는 Python 언어에 기본 제공 코드 완성을 제공합니다.
google-ads-python 라이브러리는 소스 코드의 정적 분석에 의존하는 IDE 코드 완성 기능을 억제할 수 있는 GoogleAdsClient 클래스의 getter methods를 사용하여 런타임에 protobuf 메시지 클래스를 동적으로 생성합니다.
정적 분석에 의존하는 코드 완성 도구와의 호환성을 개선하려면 다음 중 하나를 수행하면 됩니다.
- 동적 getter 메서드 와 함께 유형 주석을 사용합니다(권장).
- protobuf 메시지 클래스 및 서비스 클라이언트를 직접 가져옵니다.
유형 힌트가 있는 동적 가져오기
동적 가져오기를 사용하면 라이브러리 업데이트 전반에서 코드가 탄력적으로 유지됩니다. 유형 주석을 추가하면 동적 초기화의 이점을 잃지 않고 IDE 코드 완성을 사용 설정할 수 있습니다.
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.v26.resources.types.campaign import (
Campaign,
)
client = GoogleAdsClient.load_from_storage()
# Type hints allow the IDE to provide code completion suggestions while
# retaining dynamic class generation.
campaign: Campaign = client.get_type("Campaign")
직접 가져오기
protobuf 메시지 클래스를 직접 가져올 수도 있습니다. 이 접근 방식을 사용하면 getter 메서드를 사용하지 않고도 기본적으로 IDE 코드 완성을 사용할 수 있습니다.
from google.ads.googleads.v26.resources import Campaign
# The Campaign class is imported directly, enabling the IDE to read the
# class definition and make code completion suggestions.
campaign = Campaign()
직접 가져오기의 단점
생성된 클래스를 직접 가져오면 코드 완성에 도움이 되지만 다음과 같은 몇 가지 절충안도 도입됩니다.
- 찾기 어려움: 지정된 클래스가 있는 모듈이 항상 명확하지 않으므로 올바른 가져오기 경로를 찾기가 어려울 수 있습니다.
- 업그레이드 시 취약함: 생성된 클래스의 디렉터리 구조는 클라이언트 라이브러리의 새 버전으로 변경될 수 있습니다. 클래스를 직접 가져오면 업그레이드할 때 코드가 중단될 수 있지만 getter 메서드를 사용하는 코드는 이러한 유형의 변경사항에 영향을 받지 않습니다.
수동 초기화 필요:
get_service메서드는 인증, 전송 설정, gRPC 인터셉터(예:login-customer-id및linked-customer-id헤더 연결, 로깅 처리,GoogleAdsException에서 오류 래핑)로 서비스를 초기화한 후 반환합니다. 서비스를 직접 가져오고 인스턴스화하면 이러한 인터셉터가 우회되고 사용자 인증 정보와 전송 채널을 수동으로 구성해야 합니다.from google.ads.googleads.v26.services.services import ( campaign_service as campaign_service_module, ) # Dynamic service getter (initializes credentials, transport, and gRPC # interceptors automatically). campaign_service = client.get_service("CampaignService") # Direct service client import (requires manual credential and interceptor # configuration). campaign_service_client = campaign_service_module.CampaignServiceClient( credentials=client.credentials, )