[Python] Incompatible types in assignment while type hinting Python3

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    For a simple py code when I am using the type it throws me the following error:
    error: Incompatible types in assignment (expression has type "object", variable has type "List[str]")

    I have tried replacing List[str] with List[AnyStr] but still it doesnt helps.

    The following is the skeleton of the func:

    import uuid
    from typing import List
    import numpy as np
    def plot_X(
    X: List[np.array],
    Y: List[str], ### Line throwing error
    name: str = str(uuid.uuid4()),
    xlabel: str = "Values",
    ylabel: str = "Likelihood",
    title: str = "Title",
    X_to_plot: int = 10,
    show=False,
    ) -> None:

    Y = Y if Y else range(len(X))
    if len(X) > X_to_plot:
    idxs = np.random.randint(0, len(X), X_to_plot, replace=False)
    X = X[idxs]
    Y = Y[idxs] if Y else range(X_to_plot)

    X = X[:X_to_plot]
    Y = Y[:X_to_plot]

    fig, ax = plt.subplots(figsize=(14, 12))

    for i in range(len(X)):
    ax.plot(X, linewidth=1.5, label=f"{Y}")

    Continue reading...

Compartilhe esta Página