question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Appearance of the object is that it's an empty dict, with a custom repr that would be solved

See original GitHub issue

If you want I could make a PR that would look like the following:

>>> import holidays
>>> us_holidays = holidays.UnitedStates()
>>> us_holidays
holidays.UnitedStates(country="US", prov=None, state=None)

Wouldn’t this be nice? Let me know if I should make the PR

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
d33tahcommented, Nov 24, 2021

Hi @dr-prodigy! Thanks for this awesome library! I recently found it while looking for a solution to an ugly banking-related problem and I appreciate having an elegant solution to it.

I’d like you to reopen the issue, because I agree with @kootenpv’s point. Here’s why:

I found this library looking for a list of holidays in Poland. I didn’t expect it to work, but got intrigued by hundreds of Github stars so I tried it out. So I flick through the README, pip install it, import it and try it out in my own way:

>>> import holidays
>>> holidays.Poland()
{}

So… an empty dictionary? “That’s not a way to tell me that Poland isn’t supported!” - I thought. So I looked at the README again, saw that Poland was marked as “None” and almost moved along, assuming it’s just yet another popular Python library that doesn’t seem to work for my use case. Curious why it’s the case though, I looked at the code and found that it seems to make sense. So I tried another approach:

>>> d = {}
>>> import holidays
>>> holidays.Poland._populate(d, 2021)
>>> d
{datetime.date(2021, 1, 1): 'Nowy Rok', datetime.date(2021, 1, 6): 'Święto Trzech Króli', datetime.date(2021, 4, 4): 'Niedziela Wielkanocna', datetime.date(2021, 4, 5): 'Poniedziałek Wielkanocny', datetime.date(2021, 5, 1): 'Święto Państwowe', datetime.date(2021, 5, 3): 'Święto Narodowe Trzeciego Maja', datetime.date(2021, 5, 23): 'Zielone Świątki', datetime.date(2021, 6, 3): 'Dzień Bożego Ciała', datetime.date(2021, 8, 15): 'Wniebowzięcie Najświętszej Marii Panny', datetime.date(2021, 11, 1): 'Uroczystość Wszystkich świętych', datetime.date(2021, 11, 11): 'Narodowe Święto Niepodległości', datetime.date(2021, 12, 25): 'Boże Narodzenie (pierwszy dzień)', datetime.date(2021, 12, 26): 'Boże Narodzenie (drugi dzień)'}

Yay! Maybe I can use it after all then! You’re probably shaking your head in disagreement… rightfully so, because I’m supposed to do something like datetime.date(2021, 1,1) in holidays.Poland(). To which I’d normally reply “but what’s the point checking if I already got an empty dict…?”

So that’s basically the problem. The __repr__ is misleading and should be changed. Because right now if you come with a lesser known country such as Poland, you’re kind of used to libraries not working and you might just move along, humbly kind of agreeing with that assumed neglect.

Here are the solutions I propose:

  • like @kootenpv suggested, make __repr__ actually return a string that doesn’t suggest it’s a plain dict,
  • if you assume people rely on this behavior of __repr__ (which they probably shouldn’t) and you want to be nice to them, throw a DeprecationWarning that this __repr__ is deprecated and you’d rather say it’s an instance of holidays.countries.poland.Poland or something,
  • explicitly show in README that this {} is misleading and __contains__ is overloaded. Showing it in an interactive example is better than writing it as a part of text as some folks scan through dozens of libraries and might just omit something that doesn’t seem to work,
  • expose a regular is_holiday() method, because mixing a misleading __repr__ with overloaded __contains__ might be just a bit too much magic.
1reaction
d33tahcommented, Nov 26, 2021

@dr-prodigy yup! That seems to solve the problem 😃 And once again, thanks for implementing this library!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Initialize an Empty Dictionary in Python - GeeksforGeeks
Method 1: Use of { } symbol. We can create an empty dictionary object by giving no elements in curly brackets in the...
Read more >
Custom Python Dictionaries: Inheriting From dict vs UserDict
In this tutorial, you'll learn how to: Create dictionary-like classes by inheriting from the built-in dict class; Identify common pitfalls that ...
Read more >
A python class that acts like dict - Stack Overflow
So far, I have not seen an example that shows how to override the [] operator. So if a [] access function is...
Read more >
Python __str__() and __repr__() functions | DigitalOcean
Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the ...
Read more >
How can I determine if a dictionary is empty? - Python FAQ
Yes, there are several ways to check if a dictionary is empty. The more standard Python method is to use a Boolean evaluation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found