Skip to content

unittest.TestCase.enterContext interferes with type-checking #2325

@alanhdu

Description

@alanhdu

Describe the Bug

This code reports two errors

import unittest 
import pathlib 
import tempfile

class TestMigrationUtils(unittest.TestCase):
    def setUp(self) -> None:
        tmp_path = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))

        with tempfile.TemporaryDirectory() as tmpdir:
            pathlib.Path(tmpdir)
ERROR sandbox.py:7:83-85: No matching overload found for function `tempfile.TemporaryDirectory.__init__` called with arguments: () [[no-matching-overload](https://pyrefly.org/en/docs/error-kinds/#no-matching-overload)]
  Possible overloads:
  (suffix: str | None = None, prefix: str | None = None, dir: PathLike[str] | str | None = None, ignore_cleanup_errors: bool = False, *, delete: bool = True) -> None [closest match]
  (suffix: bytes | None = None, prefix: bytes | None = None, dir: PathLike[bytes] | bytes | None = None, ignore_cleanup_errors: bool = False, *, delete: bool = True) -> None
ERROR sandbox.py:7:83-85: `PathLike[str] | str` is not assignable to upper bound `bytes | str` of type variable `AnyStr` [[bad-specialization](https://pyrefly.org/en/docs/error-kinds/#bad-specialization)]

I'm a bit confused why the self.enterContext causes this error (especially retroactively causing an error with tempfile.TemporaryDirectory()?).

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABAK7oQMPzMA67N9ZsVQMAFlAjYmfarUZMuNSLD58AxlFRw4TACrcAwlpgAKNhy5wGhfVaNwYASkR8mbpphhgmDhgFViEwcoMEcmAFoAPiYAOVx0GBd0dxSfGBDCBhoAfWExJgBeJjzxSUIABRFRIPSwQhh0LjoDeK58BhNFYmUYGxgBOlQ6UgARCDoYNQZ6UhNHeZAAGhAWBmg4EnJEEABiJj81iQZSJjA2KYh4uFUsL1P6KhFs9BYqbBg6E3xEJghGsKiPgYdCSKQmDBYdGSYB4IBir3eIKYwHwAF9YXwliAyBMwFBSJlcFQoBQ9uVSLj8T4MDgCEw1PFIABzSEiS7oQh8PYAZRgMCYok4xDgiAA9KKcV58YR6EzRQ1RZhcGo4KKGehmay1vFRfc6ExUAA3VDQVDYWD0xkQFmDbXJXDEO0bPhkMTxcKGj5wdmFaQgADMhAAjAAmDHoECo5aoC6egBi0BgFDQWDwRDIkaAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions