Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules.datetime.date regared as str #4036

Closed
1 of 5 tasks
2rara opened this issue Oct 11, 2021 · 3 comments
Closed
1 of 5 tasks

modules.datetime.date regared as str #4036

2rara opened this issue Oct 11, 2021 · 3 comments
Labels
bug Something isn't working wontfix Not a bug or out of scope for dbt-core

Comments

@2rara
Copy link

2rara commented Oct 11, 2021

Describe the bug

Hi, after I updated dbt 0.20.0 to 0.21.0, the following code started giving an error.
(modules.datetime.date.today()-modules.datetime.timedelta(days=modules.datetime.date.today().weekday())).strftime(“%Y-%m-%d”)
Error:
‘str object’ has no attribute ‘strftime’

I think that the part before .strftime should be datetime.date in Python, so I suspect that dbt is misinterpreting the type.

Steps To Reproduce

Create a dbt macro using the code above, like
{% set monday = (modules.datetime.date.today()-modules.datetime.timedelta(days=modules.datetime.date.today().weekday())).strftime("%Y-%m-%d") %}

Run dbt compile.

Expected behavior

No error.

Screenshots and log output

'str object' has no attribute 'strftime'

System information

Which database are you using dbt with?

  • postgres
  • redshift
  • bigquery
  • snowflake
  • other (specify: ____________)

The output of dbt --version:

installed version: 0.21.0
   latest version: 0.21.0

Up to date!

Plugins:
  - bigquery: 0.21.0
  - snowflake: 0.21.0
  - redshift: 0.21.0
  - postgres: 0.21.0

The operating system you're using:
ProductName: macOS
ProductVersion: 11.6
BuildVersion: 20G165

The output of python --version:
Python 2.7.16

Additional context

I didn't get the error when I used dbt 0.20.0.

@2rara 2rara added bug Something isn't working triage labels Oct 11, 2021
@jtcohen6
Copy link
Contributor

@2rara Thanks for the detailed writeup. I haven't been able to reproduce the issue you're seeing:

-- models/my_models.sql
{% set monday = (modules.datetime.date.today()-modules.datetime.timedelta(days=modules.datetime.date.today().weekday())).strftime("%Y-%m-%d") %}

select '{{ monday }}' as today
-- target/compiled/<project_name>/models/my_models.sql


select '2021-10-11' as today

How did you install dbt? Which version of python3 are you using (python3 --version)?

@jtcohen6 jtcohen6 removed the triage label Oct 11, 2021
@2rara
Copy link
Author

2rara commented Oct 11, 2021

Hi @jtcohen6 thank you for your quick check and response!
(I'm on Python 3.8.10.)

I was looking at the wrong culprit of the error, and my current conclusion is that this is not a bug.
To emulate my dbt and database setup, if you create a file like "datatype_test.sql" with the following code under models, and run dbt compile --models datatype_test, you'll get the error I got. I can avoid the error by replacing {{v.strftime("%Y-%m-%d")}} with {{v}}.

{% set today = modules.datetime.date.today() %}
{% set monday = modules.datetime.date.today()-modules.datetime.timedelta(days=modules.datetime.date.today().weekday()) %}
{% set date_list = [] %}
{% set _ = date_list.append(today.strftime("%Y-%m-%d")) %}
{% set _ = date_list.append(monday.strftime("%Y-%m-%d")) %}

    {%- for v in date_list -%}
        '{{v.strftime("%Y-%m-%d")}}'
    {%- endfor -%}

I cannot test this with dbt@0.20.0 at the moment, but if it runs through with it, it could be a bug, and the bug might be #3499.

@jtcohen6
Copy link
Contributor

@2rara That looks right to me. You're adding strings to date_list (the output of .strftime("%Y-%m-%d")), so you cannot apply .strtime() again. Just {{v}} inside the final loop does the trick.

@jtcohen6 jtcohen6 added the wontfix Not a bug or out of scope for dbt-core label Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix Not a bug or out of scope for dbt-core
Projects
None yet
Development

No branches or pull requests

2 participants