0

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 ?

3
  • 1
    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 this case, 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) Commented Apr 22, 2020 at 11:17
  • Got it. So this is a warning of a 'might be' case & unless you are absolutely sure of the logic & data used here, this warning (not an error) is nothing to be concerned about. Am I right ? Commented Apr 22, 2020 at 11:20
  • That's correct.. I'll make that an answer. Commented Apr 22, 2020 at 11:23

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.