swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
494 stars 40 forks source link

How to use argparse.FileType? #108

Closed s3rgeym closed 1 year ago

s3rgeym commented 1 year ago
class Args(Tap):
    depth: int = 2  # max crawling depth
    visits_per_domain: int = 50  # limit visits per domain

    def configure(self) -> None:
        self.add_argument(
            "-i", "--input", help="site urls", default="-", type=argparse.FileType()
        )
❯ poetry run sqli-scanner
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/semen/workspace/sqli-scanner/sqli_scanner/cli.py", line 22, in cli
    asyncio.run(run(Args().parse_args()))
                    ^^^^^^^^^^^^^^^^^^^
  File "/home/semen/.cache/pypoetry/virtualenvs/sqli-scanner--aOpk7rM-py3.11/lib/python3.11/site-packages/tap/tap.py", line 460, in parse_args
    setattr(self, variable, deepcopy(value))
                            ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/copy.py", line 161, in deepcopy
    rv = reductor(4)
         ^^^^^^^^^^^
TypeError: cannot pickle '_io.TextIOWrapper' object
swansonk14 commented 1 year ago

Hi @s3rgeym,

Thank you for bringing up this issue! It looks like this is a problem with deepcopy, which we use to enforce immutability of default values. We have moved deepcopy earlier in the pipeline, which avoids this error. (See these commits: https://github.com/swansonk14/typed-argument-parser/commit/33e7c5311836d4d6d85beb0645b2549652966a03 and https://github.com/swansonk14/typed-argument-parser/commit/04357df392e6b15f8766b34656c206ce49a2f126). This fix will be included in the next release.

Best, Kyle and Jesse