IDE 코드 완성

VS Code 및 PyCharm과 같은 IDE는 Python 언어에 대한 기본 제공 코드 완성을 제공합니다.

google-ads-python 라이브러리는 GoogleAdsClient 클래스의 getter 메서드를 사용하여 런타임에 protobuf 메시지 클래스를 동적으로 생성하므로 소스 코드의 정적 분석에 의존하는 IDE 코드 완성 기능을 방해할 수 있습니다.

정적 분석을 사용하는 코드 완성 도구와의 호환성을 개선하려면 다음 중 하나를 실행하면 됩니다.

  1. 동적 getter 메서드와 함께 유형 주석을 사용합니다(권장).
  2. 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()

직접 가져오기의 단점

생성된 클래스를 직접 가져오면 코드 완성에 도움이 되지만 다음과 같은 단점도 있습니다.

  1. 찾기 어려움: 지정된 클래스가 어느 모듈에 있는지 명확하지 않은 경우가 있으므로 올바른 가져오기 경로를 찾기 어려울 수 있습니다.
  2. 업그레이드 시 취약함: 생성된 클래스의 디렉터리 구조는 클라이언트 라이브러리의 새 버전에 따라 변경될 수 있습니다. 클래스를 직접 가져오면 업그레이드 시 코드가 손상될 수 있지만, getter 메서드를 사용하는 코드는 이러한 유형의 변경사항에 영향을 받지 않습니다.
  3. 수동 초기화 필요: 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,
    )