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

Custom holidays not iterable #685

Closed
steakhutzeee opened this issue Jul 29, 2022 · 4 comments · Fixed by #691
Closed

Custom holidays not iterable #685

steakhutzeee opened this issue Jul 29, 2022 · 4 comments · Fixed by #691

Comments

@steakhutzeee
Copy link

steakhutzeee commented Jul 29, 2022

Hello,

i added some holidays to the "IT" ones this way:

class my_holidays:
    def _populate(self, year):

        holidays.IT._populate(self, year)

        # Add custom holidays
        self[date(year, 6, 24)] = "San Giovanni Battista"
        self[date(year, 12, 31)] = "San Silvestro"

But when i try to call it:
Note: holidays is a personal custom list, current_day is a variable and i also tested it directly in the interpreter but issue is the same. No issue with the standard it_holidays = holidays.IT()

# Check if current day is an holiday
if x > 0 and current_day in my_holidays:
    holidays.append(
        (
           days_list[x],
           datetime.strptime(current_day, "%m/%d/%y").strftime("%d/%m/%Y"),
        )
    )

i receive the following error:

if x > 0 and current_day in my_holidays:
TypeError: argument of type 'type' is not iterable

Any idea what am i doing wrong?

Thanks!

@dr-prodigy
Copy link
Collaborator

dr-prodigy commented Jul 30, 2022

Hi,
I kindly ask you to follow the provided doc and, when possible, avoid opening project issues about specific implementation issues.
This said, in order to make it work, you should at least inherit your class from Italy class, like so (I'm from mobile so there could be some small error / typo):

...
class my_holidays (holidays.Italy):
...

Then, if your added holidays are valid for the whole Italy or a province, I would suggest to prepare a PR to include them in the lib code.

Thx, best

@steakhutzeee
Copy link
Author

steakhutzeee commented Jul 30, 2022

Before implementing this in my personal code i tried the exact code from the documentation in the section "Creating custom holidays (or augmenting existing ones with private ones)" and got same issue discussed here https://github.com/dr-prodigy/python-holidays/issues/500

That's why i used:

class my_holidays:

instead of:

class my_holidays (holidays.Itay):

If i use the code from the docs:

>>> import holidays
>>> from datetime import date
>>> class CorporateHolidays(holidays.US):
...     def _populate(self, year):
...             holidays.US._populate(self, year)
...             self[date(year, 7, 13)] = "Ninja Turtle Day"
>>> date(2014, 10, 14) in CorporateHolidays(country="US")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\holidays\countries\united_states.py", line 107, in __init__
    HolidayBase.__init__(self, **kwargs)
TypeError: HolidayBase.__init__() got an unexpected keyword argument 'country'

That is exactly the error from the link i provided.

If i try to follow the solution provided in the link:

>>> class CorporateHolidays():
...     def _populate(self, year):
...             holidays.US._populate(self, year)
...             self[date(year, 7, 13)] = "Ninja Turtle Day"

I get exactly the same issue i have in my custom code:

>>> date(2014, 10, 14) in CorporateHolidays()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'CorporateHolidays' is not iterable

So there is probably something i'm not doing right :/

@dr-prodigy
Copy link
Collaborator

At a further check, I confirm that specific example referred to is providing a wrong code: being already a country-inherited object, the subsequent call to country="US" is not valid, thus the first reported error.

Going to the second code, on the other hand, the inheritance is still obviously required: no examples in the doc is omitting it.

The correct code is then

import holidays
from datetime import date

class CorporateHolidays(holidays.US):
  def _populate(self, year):
    holidays.US._populate(self, year)
      self[date(year, 7, 13)] = "Ninja Turtle Day"

print (date(2014, 7, 13) in CorporateHolidays())

The doc will be fixed accordingly, thx for your clarifications!

dr-prodigy added a commit that referenced this issue Jul 30, 2022
@dr-prodigy
Copy link
Collaborator

The fix has been just pushed on beta.
It will be then published to https://python-holidays.readthedocs.io when merged on master, ie: when the next app official version (0.15) is released.
Closing this: pls reach out in case of further needs, thx.

@dr-prodigy dr-prodigy mentioned this issue Aug 21, 2022
dr-prodigy added a commit that referenced this issue Aug 21, 2022
* add honduras tests

* add honduras tests

* Add tests for many edge cases

* Add tests for many edge cases

* remove duplicate function

* v.0.15 beta init

* Typechecking (#661)

* typechecking HolidayBase, holidays.utils, miscellaneous typechecking
note: coverage to be extended to other parts of the project code

* Update Ukraine holidays.

  - Add Day of Ukrainian Statehood holiday.
  - Edit Victory Day name.
  - Fix formatting.
  - Update tests for Ukraine holidays.
  - Switch to <= and >= for consistency.
  - Rename Orthodox Easter.

* CHANGES sync

* Add Day after Thanksgiving to US - PA holidays (#675)

* CHANGES sync

* CHANGES sync, pre-commit reviews

* test_ukraine pre-commit fix

* Bump actions/setup-python from 3.1.0 to 4.1.0 (#682)

* Bump pre-commit/action from 2.0.3 to 3.0.0 (#672)

* removed py36 from toml target-version array

* Add Cuba (#678)

authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* Make Colombian holidays preserve historical observation (#676)

Authored-by: bthompson <bthompson@pinterest.com>

* Make Venezuela holidays preserve historical observation (#677)

Authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* black fixes

* removed outdated deprecation warnings

* Drop support for PortugalExt

* Ensure Canada Christmas observed falls after 25 and not before - new version #579

* CHANGES sync

* README sync (support for Cuba)

* Drop support for UK subdivisions as countries (England, Scotland..) (dr-p)
Drop support for IsleOfMan as UK subdivision (dr-p)

* Added support for Python 3.11, improved README badges

* Fixed CD/CI setup.cfg typo

* Added py3.11 to toml config

* Removed py3.11 from setup.cfg due to failing pre-commit checks

* Bump pypa/gh-action-pypi-publish from 1.5.0 to 1.5.1 #686

* Doc example fix (#685)

* added pentecost monday for province of bolzano

source: https://it.wikipedia.org/wiki/Pentecoste

* Whitsun Monday - province of Bolzano - IT (#689)

* Bump actions/setup-python to 4.2.0 (#688)

* v.0.15

Co-authored-by: Akos <afurton@apple.com>
Co-authored-by: Akos Furton <furton.akos@bcg.com>
Co-authored-by: Maurizio Montel <mmontel@mmontel.mxp.csb>
Co-authored-by: David Hotham <david.hotham@blueyonder.co.uk>
Co-authored-by: Kate <kate@kgthreads.com>
Co-authored-by: Ark <ark@cho.red>
Co-authored-by: Bailey Thompson <bailey.kyle.thompson@gmail.com>
Co-authored-by: g-gg <52978382+g-gg@users.noreply.github.com>
dr-prodigy added a commit that referenced this issue Sep 16, 2022
* add honduras tests

* add honduras tests

* Add tests for many edge cases

* Add tests for many edge cases

* remove duplicate function

* v.0.15 beta init

* Typechecking (#661)

* typechecking HolidayBase, holidays.utils, miscellaneous typechecking
note: coverage to be extended to other parts of the project code

* Update Ukraine holidays.

  - Add Day of Ukrainian Statehood holiday.
  - Edit Victory Day name.
  - Fix formatting.
  - Update tests for Ukraine holidays.
  - Switch to <= and >= for consistency.
  - Rename Orthodox Easter.

* CHANGES sync

* Add Day after Thanksgiving to US - PA holidays (#675)

* CHANGES sync

* CHANGES sync, pre-commit reviews

* test_ukraine pre-commit fix

* Bump actions/setup-python from 3.1.0 to 4.1.0 (#682)

* Bump pre-commit/action from 2.0.3 to 3.0.0 (#672)

* removed py36 from toml target-version array

* Add Cuba (#678)

authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* Make Colombian holidays preserve historical observation (#676)

Authored-by: bthompson <bthompson@pinterest.com>

* Make Venezuela holidays preserve historical observation (#677)

Authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* black fixes

* removed outdated deprecation warnings

* Drop support for PortugalExt

* Ensure Canada Christmas observed falls after 25 and not before - new version #579

* CHANGES sync

* README sync (support for Cuba)

* Drop support for UK subdivisions as countries (England, Scotland..) (dr-p)
Drop support for IsleOfMan as UK subdivision (dr-p)

* Added support for Python 3.11, improved README badges

* Fixed CD/CI setup.cfg typo

* Added py3.11 to toml config

* Removed py3.11 from setup.cfg due to failing pre-commit checks

* Bump pypa/gh-action-pypi-publish from 1.5.0 to 1.5.1 #686

* Doc example fix (#685)

* added pentecost monday for province of bolzano

source: https://it.wikipedia.org/wiki/Pentecoste

* Whitsun Monday - province of Bolzano - IT (#689)

* Bump actions/setup-python to 4.2.0 (#688)

* v.0.15

* v.0.16 beta init

* Add UK Holiday for Queen Elizabeth II Funeral (#702)

* Add Moldova (#695)

* Add Bolivian holidays (#698)

* CHANGES sync

* Fix NYSE New Year observance for Saturdays. (#696)

* CHANGES sync

* Financial market support review, new method financial_holidays(..) #694

* Add Australia National Day of Mourning and confirmed Grand Final Day date (#699)

* CHANGES sync

* Add queen's funeral holidays for canada (#710)

* CHANGES sync

* New Zealand updates for Queen Elizabeth II mourning + King Charles III (#708, #709)

* Australia - new king updates

* Fix typo in comment for Cuba (#712)

* CHANGES sync - v.0.16 release date, dedication

Co-authored-by: Akos <afurton@apple.com>
Co-authored-by: Akos Furton <furton.akos@bcg.com>
Co-authored-by: Maurizio Montel <mmontel@mmontel.mxp.csb>
Co-authored-by: David Hotham <david.hotham@blueyonder.co.uk>
Co-authored-by: Kate <kate@kgthreads.com>
Co-authored-by: Ark <ark@cho.red>
Co-authored-by: Bailey Thompson <bailey.kyle.thompson@gmail.com>
Co-authored-by: g-gg <52978382+g-gg@users.noreply.github.com>
Co-authored-by: Jacob Punter <54587331+JPunter@users.noreply.github.com>
Co-authored-by: Nikita <theedandroid@gmail.com>
Co-authored-by: Ryan McCrory <ryan.mccrory92@gmail.com>
dr-prodigy added a commit that referenced this issue Nov 13, 2022
* add honduras tests

* add honduras tests

* Add tests for many edge cases

* Add tests for many edge cases

* remove duplicate function

* v.0.15 beta init

* Typechecking (#661)

* typechecking HolidayBase, holidays.utils, miscellaneous typechecking
note: coverage to be extended to other parts of the project code

* Update Ukraine holidays.

  - Add Day of Ukrainian Statehood holiday.
  - Edit Victory Day name.
  - Fix formatting.
  - Update tests for Ukraine holidays.
  - Switch to <= and >= for consistency.
  - Rename Orthodox Easter.

* CHANGES sync

* Add Day after Thanksgiving to US - PA holidays (#675)

* CHANGES sync

* CHANGES sync, pre-commit reviews

* test_ukraine pre-commit fix

* Bump actions/setup-python from 3.1.0 to 4.1.0 (#682)

* Bump pre-commit/action from 2.0.3 to 3.0.0 (#672)

* removed py36 from toml target-version array

* Add Cuba (#678)

authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* Make Colombian holidays preserve historical observation (#676)

Authored-by: bthompson <bthompson@pinterest.com>

* Make Venezuela holidays preserve historical observation (#677)

Authored-by: bthompson <bthompson@pinterest.com>

* CHANGES sync

* black fixes

* removed outdated deprecation warnings

* Drop support for PortugalExt

* Ensure Canada Christmas observed falls after 25 and not before - new version #579

* CHANGES sync

* README sync (support for Cuba)

* Drop support for UK subdivisions as countries (England, Scotland..) (dr-p)
Drop support for IsleOfMan as UK subdivision (dr-p)

* Added support for Python 3.11, improved README badges

* Fixed CD/CI setup.cfg typo

* Added py3.11 to toml config

* Removed py3.11 from setup.cfg due to failing pre-commit checks

* Bump pypa/gh-action-pypi-publish from 1.5.0 to 1.5.1 #686

* Doc example fix (#685)

* added pentecost monday for province of bolzano

source: https://it.wikipedia.org/wiki/Pentecoste

* Whitsun Monday - province of Bolzano - IT (#689)

* Bump actions/setup-python to 4.2.0 (#688)

* v.0.15

* v.0.16 beta init

* Add UK Holiday for Queen Elizabeth II Funeral (#702)

* Add Moldova (#695)

* Add Bolivian holidays (#698)

* CHANGES sync

* Fix NYSE New Year observance for Saturdays. (#696)

* CHANGES sync

* Financial market support review, new method financial_holidays(..) #694

* Add Australia National Day of Mourning and confirmed Grand Final Day date (#699)

* CHANGES sync

* Add queen's funeral holidays for canada (#710)

* CHANGES sync

* New Zealand updates for Queen Elizabeth II mourning + King Charles III (#708, #709)

* Australia - new king updates

* Fix typo in comment for Cuba (#712)

* CHANGES sync - v.0.16 release date, dedication

* Fix Honduras

* Add 2014 test honduras

* Rename Eswatini from Swaziland

* Add import

* Add pre-commit isort hook.

* Run `pre-commit run --all-files`.

* Fix black - isort conflict.

* Revert docs conf.py changes.

* Sort

* Sort

* Introduce special holidays support.

Extend `HolidayBase` in order to make special holidays handling simpler.

* Fix documentation and UK special holidays list.

* Add a test.

* Add Bosnia and Herzegovina holidays.

* Edit Eid ul-Adha holiday logic and improve code readability.

* Improve readability.

* Add more tests.

* Update README.rst

* Change `special_holidays` format.

* Add Liechtenstien holidays and tests.

* Refactor tests.

* Remove empty constructors.

Move country holidays sources/descriptions from comments to docstrings.

* v.0.17 beta init

* Sort holiday names. (#713)

* Sort holiday names.
Order holidays by name when there are multiple holidays on the same date.

* CHANGES sync

* CHANGES sync

* Supported countries no. update, mexico.py permissions fix

* Keep context based imports sorting for `holidays.constants`.

* Fix observed holiday for christmas and boxing day canada (#715)

* CHANGES sync

* .pre-commit-config.yaml update

* Get holidays range type fix

* CHANGES sync

* README.rst: Fix documentation about Portugal subdiv param (#742)

* Bump actions/setup-python from 4.2.0 to 4.3.0 (#740)

Bumps [actions/setup-python](https://github.com/actions/setup-python) from 4.2.0 to 4.3.0.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](actions/setup-python@v4.2.0...v4.3.0)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Fixed incorrect subdivision code (#733)

* CHANGES sync

* Run isort.

* Bug fix of substitute holidays in Japan (#723)

* CHANGES sync

* isort commit

* isort commit

* CHANGES sync #650, #728

* Fix Honduras Morazan weekend bug. (#744)

* CHANGES sync

* README fix for Liechtenstein

* Singapore: Revised Date for Vesak Day in 2023 (#726)

* CHANGES sync

* Update malaysia.py (#736)

fix month for deewali 2022

* CHANGES sync

* README fix: pip install --upgrade (#735)

* Update Ukrainian holidays (#743)

* CHANGES sync

* Redesign special holidays population logic.

* Fix `_populate` docstring.

* README.rst: Remove Alpha-3 code for Bosnia and Herzegovina (#755)

Adding an extra entry like the Alpha-3 code in the countries
table causes render failure

* Added observed holidays for Ukraine (#746)

* Implement 100% test coverage (#747)

* Fix a typo: Liechtenstien -> Liechtenstein. (#752)

* Fixed Easter holidays for Bulgaria (#748)

* CHANGES sync

* pyproject.toml fixed, Ukraine reformatting

* CHANGES sync

* Portugal: add district holidays (#753)

Added holidays for the 18 portuguese districts
Added tests
Updated README.rst

* Import isort reformatting

* Fix Diwali date for 2022. (#754)

* CHANGES sync

* Optimizations for Ireland (#756)

* CHANGES sync

* Remove extra spaces from strings (#759)

* CHANGES sync

* Mexico: optimizations (#760)

* CHANGES sync

* Germany: optimizations (#766)

* China: optimizations (#767)

* CHANGES sync

* Fix a couple of `_populate` related issues.

* United Kingdom: optimizations (#780)

* Ukraine: observed holidays calculation more universal and readable (#776)

* Canada: optimizations, Yukon Heritage Day fix (#775)

* CHANGES sync

* Clean up Ireland tests. (#765)

Remove `print` statement.

* add documentation to _populate (see #704) (#763)

* Fix observance logic for laborable holidays in Uruguay (#758)

* Fix `list_supported_countries` and `list_supported_financial` methods. (#764)

Replace `obj.__base__` with `issubclass`.
Add Isle Of Man alpha-3 code.

* brazil.py: Update SC holiday according 2017 law (#761)

From 2017 onwards if the 11th of August falls on a work day the
holiday is moved to the next Sunday.

Issue: 592

* Added special case on 2022 (#779)

Added special case on 2022 Chinese Mid-Autumn Festival

Added test case on test_hongkong.py

* Remove `assert` from Norway and Sweden. (#771)

Rework all Sundays collection logic (use dateutil.rrule).
Extract repetitive test_sundays code to a separate file.

* South Africa: (#773)

- replace while loops with dateutil logic (#768)
- Queen's Birthday day fix
- observed holidays calculation fix
- added tests

* Replace Python built-in asserts with unittest asserts. (#777)

* CHANGES sync

* Replace while loops with dateutil logic (#768) (#770)

* CHANGES sync

* Lithuania: add the 2nd on November as holiday (#781)

* CHANGES sync

* MyPy implicit optional option

* Update Hong Kong holiday, mid-autumn (#782)

* CHANGES sync

* Sweden: fix Midsummer evening (#783)

* CHANGES sync

* v0.17

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Akos <afurton@apple.com>
Co-authored-by: Akos Furton <furton.akos@bcg.com>
Co-authored-by: Maurizio Montel <mmontel@mmontel.mxp.csb>
Co-authored-by: David Hotham <david.hotham@blueyonder.co.uk>
Co-authored-by: Kate <kate@kgthreads.com>
Co-authored-by: Ark <ark@cho.red>
Co-authored-by: Bailey Thompson <bailey.kyle.thompson@gmail.com>
Co-authored-by: g-gg <52978382+g-gg@users.noreply.github.com>
Co-authored-by: Jacob Punter <54587331+JPunter@users.noreply.github.com>
Co-authored-by: Nikita <theedandroid@gmail.com>
Co-authored-by: Ryan McCrory <ryan.mccrory92@gmail.com>
Co-authored-by: Pedro Baptista <32106544+Nalguedo@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Michael Thessel <der.thes@gmail.com>
Co-authored-by: Sho Hirose <sho.hirose@gmail.com>
Co-authored-by: Mike Borsetti <26698111+mborsetti@users.noreply.github.com>
Co-authored-by: shahonseven <shahonseven@gmail.com>
Co-authored-by: ~Jhellico <KJhellico@users.noreply.github.com>
Co-authored-by: poshingchu <59795066+poshingchu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants