Repository: OpenBB-finance/OpenBBTerminal
File: openbb_platform/providers/yfinance/openbb_yfinance/models/equity_screener.py (Python)

Code before:
        default=None,
        description="Filter by a beta greater than this value.",
    )
    beta_max: float | None = Field(
        default=None,
        description="Filter by a beta less than this value.",
    )
    limit: int | None = Field(
        default=200,
        description="Limit the number of results returned. Default is, 200. Set to, 0, for all results.",
    )

    @field_validator("country", mode="before")
    @classmethod
    def _validate_country(cls, v):
        """Validate and normalize country input."""
        if v is None or v == "all":
            return v
        # Convert Country type or string to lowercase alpha-2 for YFinance
        if isinstance(v, Country):
            country_code = v.alpha_2.lower()
        else:
            try:
                country_code = Country(v).alpha_2.lower()
            except ValueError:
                return v  # Let it fail at API level if invalid
        if country_code not in COUNTRIES:
            raise ValueError(
                f"Country '{v}' ({country_code.upper()}) is not supported by YFinance. "
                f"Valid options: {', '.join(sorted(COUNTRIES)[:20])}...",
            )
        return country_code


class YFinanceEquityScreenerData(EquityScreenerData, YFPredefinedScreenerData):
    """YFinance Equity Screener Data."""


class YFinanceEquityScreenerFetcher(
    Fetcher[YFinanceEquityScreenerQueryParams, list[YFinanceEquityScreenerData]],
):
    """YFinance Equity Screener Fetcher."""

    @staticmethod
    def transform_query(params: dict[str, Any]) -> YFinanceEquityScreenerQueryParams:
        """Transform query."""
        sector = params.get("sector")
        industry = params.get("industry")

        if industry and sector:
            sec = get_industry_sector(industry)

Review comment (bug):
If the value fails to parse as a Country, it likely is not a valid string. Instead of letting it through to the API call, we should validate the value against the list of accepted countries.

```suggestion
                country_code = v.strip().lower()
```

Without the raised ValueError, the user gets returned an EmptyDataError instead, which is missing the helpful message with the valid options.

Code after:
        default=None,
        description="Filter by a beta greater than this value.",
    )
    beta_max: float | None = Field(
        default=None,
        description="Filter by a beta less than this value.",
    )
    limit: int | None = Field(
        default=200,
        description="Limit the number of results returned. Default is, 200. Set to, 0, for all results.",
    )

    @field_validator("country", mode="before")
    @classmethod
    def _validate_country(cls, v):
        """Validate and normalize country input."""
        if v is None or v == "all":
            return v
        # Convert Country type or string to lowercase alpha-2 for YFinance
        if isinstance(v, Country):
            country_code = v.alpha_2.lower()
        else:
            try:
                country_code = Country(v).alpha_2.lower()
            except ValueError:
                country_code = v.strip().lower()
        if country_code not in COUNTRIES:
            raise ValueError(
                f"Country '{v}' ({country_code.upper()}) is not supported by YFinance. "
                f"Valid options: {', '.join(sorted(COUNTRIES))}",
            )
        return country_code


class YFinanceEquityScreenerData(EquityScreenerData, YFPredefinedScreenerData):
    """YFinance Equity Screener Data."""


class YFinanceEquityScreenerFetcher(
    Fetcher[YFinanceEquityScreenerQueryParams, list[YFinanceEquityScreenerData]],
):
    """YFinance Equity Screener Fetcher."""

    @staticmethod
    def transform_query(params: dict[str, Any]) -> YFinanceEquityScreenerQueryParams:
        """Transform query."""
        sector = params.get("sector")
        industry = params.get("industry")

        if industry and sector:
            sec = get_industry_sector(industry)