Metadata
Metadata

Reputation: 2083

How to read and understand 'Unexpected type(s)' problem in Python?

I am trying to split a String based on the symbol: | and further split each String in it on a : and convert the result into a dictionary as below.

column_data = 'component_id:numeric(15,0)|name:character varying(30)|operation_num:numeric(15,0)|component_item:numeric(15,0)|item_number:character varying(800)|last_update_date:timestamp without time zone|last_updated_by:numeric(15,0)|creation_date:timestamp without time zone|created_by:numeric(15,0)|item_num:numeric|component_quantity:numeric|component_yield_factor:numeric|component_remarks:character varying(240)|effectivity_date:date|change_notice:character varying(10)'

column_names = dict(item.split(":") for item in gp_column_data.split("|"))

But I see a warning on the IDE that says:

Unexpected type(s): (Generator[List[str], Any, None]) Possible types: (Mapping) (Iterable[Tuple[Any, Any]]) 

The image can be seen below: enter image description here

The same logic worked fine on Python shell but when I put the logic on IDE, the line highlights. gp_column_data is a str that I am receiving as a method parameter.

def fix_source_columns(gp_column_data: str, precision_columns:str):
    column_names = dict(item.split(":") for item in gp_column_data.split("|"))

I am new to Python and see these messages often on IDE. Could anyone let me know if this is an error message ? If so how can I fix the problem ?

Upvotes: 0

Views: 2846

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

The IDE is smart enough to warn you that item.split(":") might output an iterable with a single element in case item does not contain : (or more than 2 if there are multiple :). In these cases, dict will fail because it expects an iterable in which all elements have exactly 2 elements (which is exactly what Iterable[Tuple[Any, Any]] in the warning means).

Upvotes: 1

Related Questions