Re: can you improve this text-only beginner copy program?

"Peter J. Holzer" <[email protected]>
Newsgroups gmane.comp.python.general
Message-ID <z6qh2yjq5gqupfa5jcbtuguzr4ehyu7ld5aghy23sf5f2j3szn@ttw4dlx7gp27>
On 2025-08-28 21:15:11 +0100, Mark Bourne wrote:
> I don't know if Sphinx can extract types from type hints but, at least if
> the docstring is just for humans reading the code, using those can reduce
> the markup in the docstring:
> ```
> def copy(s: int, d: int) -> None:
>     """
>     Copy the contents of the text file whose path is given by the
>     parameter ``s`` to the text file whose path is given by ``d``.

I'm almost sure that you didn't intend the paths to be of type int.

> 
>     :param s: Path to the source text file to copy from.
>     :param d: Path to the destination text file to copy to.
>     :raises OSError: If an I/O error occurs during writing. On error,
>                      the destination file will be removed if it was
>                      partially written.
> """
> ```
> 
> Aside from acting as documentation of the expected argument and return
> types, type hints can also be read by type checkers to help find places
> where objects of the wrong types might be passed.  Type hints don't make any
> difference at runtime, so you could still call `copy(1, 2)` and it'll copy a
> file named "1" to a file named "2",

No, it would try to copy from file descriptor 1 to file descriptor 2.
Given that file descriptor 1 is normally stdout and file descriptor 2 is
stderr, this doesn't make much sense but it would work (fsvo) in a Linux
terminal.


> but type checkers would flag that as a
> possible bug - it should be `copy("1", "2")` if that's really what you
> intend.

Actually, the way you wrote it, the checker would flag the correct usage
as incorrect (and the incorrect one, too, since os.remove doesn't accept
an int).

        hjp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmjVk34ACgkQ8g5IURL+
KF2wkxAAnjEOz5nSM7zpifDlwx99dNMP4VUdVrcuLF5c9tsGbtwScfSNZwL9F3SA
jje1GhrzPGaI5ylO7R0bGWeUXOxoD5LaqML+g5uyh/C51iV2fMoVe5jM+GjrPMOq
+W7+tLz9jr2Ewrn9xG3qfwAjVSvD5VwbO0aEmAzJQQG3m9S/byEyNp5cVXt68iWI
Z25sfXyh8bY3bwS6X3ZcVU4Razv8qgthe5uj7aO4UHYo+0bicPz9Ooph12ieiUV9
B/Acq8TEzSmSwZkdYMwXcVk1OfWhbgB6ugP0ae8XGuHFKV1y7LuvhwqBMcb2HXty
QaARFB+44pBii5bYfYlcvgWtT7+Lmd3mK+iaOT3S0+sy99ERCccjsUKTiZCfBxCU
Nd4PRv6W1dqF8tAYfCE2SXwrsbL5ukfOypxdDJLpXv3Njo/QYzldGynvR9BpSFb3
hU7A8uv/EEvVSkJHhCDvP/HONxati/CS7he6LSb7n+qHdn0L/Z4uiOUN4I/prpLE
FcRH9fRfChOUJjoX2mjiiF7Piuy3UIiWydL1Lkpj+tZR+JoEj7C0dhv80Bt5kM7Z
+vM3NQYS0YXuIJyl0mvq/1/i+GogOuL9PAF7iKkvrNncpJyO37kwfAPa0AYRwrkm
nIe0nW11h1qGl+EJnf7xVQyvYyqDjmnNMDStGezF09IIkhWyhO0=
=BCa/
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.