Readit News logoReadit News
adamchainz commented on Python Type Hints – *args and **kwargs (2021)   adamj.eu/tech/2021/05/11/... · Posted by u/ekiauhce
hk__2 · 2 years ago
> In the function body, args will be a tuple, and kwargs a dict with string keys.

This always bugs me: why is `args` immutable (tuple) but `kwargs` mutable (dict)? In my experience it’s much more common to have to extend or modify `kwargs` rather than `args`, but I would find more natural having an immutable dict for `kwargs`.

adamchainz · 2 years ago
Yeah, that is odd. Python still has no immutable dict type, except it kinda does: https://adamj.eu/tech/2022/01/05/how-to-make-immutable-dict-...
adamchainz commented on Python Type Hints – *args and **kwargs (2021)   adamj.eu/tech/2021/05/11/... · Posted by u/ekiauhce
m3047 · 2 years ago
That article promulgates a misunderstanding about immutability. For my way of thinking, python is already an interpreted language and I can enforce tropes in code more cleanly and effectively than people taking something five levels up at face value and trying to figure out what sticks when they throw it against the wall: no wonder they end up frustrated, and it's a frustrating situation.

Given:

    def foo(*args):
        print(args)
        return

    class Thing(object):
        def __init__(self,a,b):
            self.a = a
            self.b = b
            return
        
        def foo_style(self):
            return (self.a, self.b)
args is not required to refer to a tuple:

    >>> foo(*[31,42])
    (31, 42)
I can have objects construct parameters conforming to the specifications for a signature:

    >>> foo(*Thing(3,91).foo_style())
    (3, 91)
Consider that a counterexample.

adamchainz · 2 years ago
Within the function, args is a tuple, as your output demonstrates.
adamchainz commented on Python Type Hints – How to Handle Optional Imports   adamj.eu/tech/2021/12/29/... · Posted by u/rbanffy
dragonwriter · 4 years ago
The thing listed as “A Non-Functioning Alternative” seems to work correctly in both Pylance and mypy, if you add “# type: ignore” at the end of the import line.
adamchainz · 4 years ago
Unfortunately that prevents Mypy from loading the type hints for 'markdown'

    from types import ModuleType

    markdown: ModuleType | None

    try:
        import markdown  # type: ignore [no-redef]
    except ImportError:
        markdown = None

    if markdown is not None:
        reveal_type(markdown.markdown)
Running Mypy:

    example.py:11: note: Revealed type is "Any"
Whereas when correctly imported:

    example.py:3: note: Revealed type is "def (text: builtins.str, *, extensions: Union[typing.Sequence[Union[builtins.str, markdown.extensions.Extension]], None] =, extension_configs: Union[typing.Mapping[builtins.str, typing.Mapping[builtins.str, Any]], None] =, output_format: Union[Literal['xhtml'], Literal['html'], None] =, tab_length: Union[builtins.int, None] =) -> builtins.str"

adamchainz commented on Python Type Hints – How to Handle Optional Imports   adamj.eu/tech/2021/12/29/... · Posted by u/rbanffy
bxparks · 4 years ago
I had to do a similar hack recently. The 'zoneinfo' library is available only on Python >=3.9. But the 'backports.zoneinfo' library is available for older Pythons. The recommended way of handling this causes mypy to spit out errors:

  try:
      import zoneinfo
  except:
      from backports import zoneinfo
The solution that I found to make mypy happy is:

  import sys
  if sys.version_info >= (3, 9):
      import zoneinfo
  else:
      from backports import zoneinfo
I have no idea why mypy is happy.

If I can get type hinting to work, it does detect certain errors in certain situations. But the whole thing takes too much work. The syntax is awkward and klunky. I'm not always sure that mypy actually catches typing errors, even with the --strict flag. And Python package management is already a shit show, but when I add mypy to the mix, it spews incomprehensible error messages that I have no idea how to fix.

I'm starting to look at Golang to replace my Python usage. If I'm going to do the work of adding typing info, I'd rather do it in a language that has static type checking from the very beginning. And being able to compile down to a single binary is appealing.

adamchainz · 4 years ago
Ah yes, nice hint that backports can use sys.version_info.
adamchainz commented on Python Type Hints – How to Handle Optional Imports   adamj.eu/tech/2021/12/29/... · Posted by u/rbanffy
wodenokoto · 4 years ago
Would it work if you used importlib?
adamchainz · 4 years ago
It will work but Mypy won't know to use the type hints for the markdown module.
adamchainz commented on Django 4.0 release candidate 1 released   djangoproject.com/weblog/... · Posted by u/pauloxnet
adamchainz · 4 years ago
Here’s my post summarizing new testing features in Django 4.0: https://adamj.eu/tech/2021/09/28/new-testing-features-in-dja...
adamchainz commented on htmx – high power tools for HTML - v1.0.0 Release   github.com/bigskysoftware... · Posted by u/sandebert
adamchainz · 5 years ago
Great news. Have been watching for a while and looking forward to using it now that it's fully released.
adamchainz commented on Plotnine: Grammar of Graphics for Python   datascienceworkshops.com/... · Posted by u/jeroenjanssens
adamchainz · 6 years ago
Awesome, basically a free book chapter. Will definitely fire up the notebook and give this a try.
adamchainz commented on Problem solving with Unix commands   vegardstikbakke.com/unix/... · Posted by u/v3gas
adamchainz · 7 years ago
I learnt a lot from the book Data Science at the Command Line, now free and online at https://www.datascienceatthecommandline.com/

u/adamchainz

KarmaCake day17November 10, 2012
About
[ my public key: https://keybase.io/adamchainz; my proof: https://keybase.io/adamchainz/sigs/gtiWWj6v-5ZX76r1sRsn3mNjviNuuPIREp_8274cCTo ]
View Original