Search code examples
python-3.xgoogle-cloud-platformgoogle-bigquerybq

Expected type '_SpecialForm[str]', got 'str' instead


I'm trying to create a BQ table schema, as seen on this page

But I get a compilation error for all the mode="REQUIRED"

I didn't see anything special to import but the bq module.

Expected type '_SpecialForm[str]', got 'str' instead 

The code:

    bqServiceWrapper.create_table(_ADS_TO_REMOVE_TABLE_NAME,
                                  [
                                      bigquery.SchemaField("add_id", "STRING", mode="REQUIRED"),
                                      bigquery.SchemaField("timestamp_str", "TIMESTAMP", mode="REQUIRED"),
                                      bigquery.SchemaField("timestamp", "TIMESTAMP", mode="REQUIRED")
                                  ])

BTW does the python BQ library allows creating a table without a schema (like Java does?). If so - how can the type to implied as "TIMESTAMP" and not "STRING"?


Solution

  • There appears to be an extra mode=”REQUIRED” in your code. Also, your code is not creating a table as mentioned in the doc table = bigquery.Table(table_id, schema=schema) . Rewriting your code as follows :

    from google.cloud import bigquery
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    
    client.create_table(bigquery.Table("ProjectID.Dataset.Table", schema= [
                                       bigquery.SchemaField("add_id", "STRING", mode="REQUIRED"),
                                       bigquery.SchemaField("timestamp_str", "TIMESTAMP", mode="REQUIRED"),
                                        bigquery.SchemaField("timestamp", "TIMESTAMP", mode="REQUIRED")
                                    ]))
    
    

    This creates the table in BigQuery with the required schema :enter image description here

    For creating a schemaless table using the Python client library you can simply run the above code without the schema : client.create_table(bigquery.Table("ProjectID.Dataset.Table")) or directly client.create_table("ProjectID.Dataset.Table"). But if we are creating a schemaless table we need to define the schema either by auto-detect or manually and only then we can add data to it. Assuming you are trying to load data from a CSV file into an empty table with auto-detect schema, you need to have the Timestamp data in the supported format as specified in this doc.