Title: Conditional import of a class follows imports of all branches · Issue #19009 · python/mypy · GitHub
Open Graph Title: Conditional import of a class follows imports of all branches · Issue #19009 · python/mypy
X Title: Conditional import of a class follows imports of all branches · Issue #19009 · python/mypy
Description: Bug Report When you have the following provider module: if sys.platform == "win32": from my_pkg._pty.windows import PtySession else: from my_pkg._pty.unix import PtySession and attempt importing this symbol from somewhere else, Mypy does...
Open Graph Description: Bug Report When you have the following provider module: if sys.platform == "win32": from my_pkg._pty.windows import PtySession else: from my_pkg._pty.unix import PtySession and attempt importing th...
X Description: Bug Report When you have the following provider module: if sys.platform == "win32": from my_pkg._pty.windows import PtySession else: from my_pkg._pty.unix import PtySession and attempt im...
Opengraph URL: https://github.com/python/mypy/issues/19009
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conditional import of a class follows imports of all branches","articleBody":"**Bug Report**\n\nWhen you have the following provider module:\n\n```python\nif sys.platform == \"win32\":\n from my_pkg._pty.windows import PtySession\nelse:\n from my_pkg._pty.unix import PtySession\n```\n\nand attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.\n\nFor example, if you call `pty.openpty()` inside `my_pkg._pty.unix` and run Mypy on Windows it will show:\n\n```\nModule has no attribute \"openpty\" [attr-defined]\n```\n\n**To Reproduce**\n\nCreate the following structure:\n\n```\nmypy-issue\n├── pyproject.toml\n└── pkg\n ├── __init__.py\n ├── main.py\n └── _pty\n ├── __init__.py\n ├── interface.py\n ├── session.py\n ├── unix.py\n └── windows.py\n```\n\nwith the following contents (`__init__.py` files are empty):\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pyproject.toml\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```toml\n\n[tool.mypy]\npretty = true\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pkg/main.py\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```python\nfrom __future__ import annotations\n\n\nclass SubprocessRunner:\n def run(self) -> bool:\n from ._pty.session import PtySession\n\n return PtySession is not None\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pkg/_pty/interface.py\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```python\nfrom __future__ import annotations\n\nfrom abc import ABC, abstractmethod\n\n\nclass PtySessionInterface(ABC):\n def __init__(self, command: list[str]) -> None:\n self.command = command\n\n @abstractmethod\n def get_exit_code(self) -> int | None: ...\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pkg/_pty/session.py\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```python\nfrom __future__ import annotations\n\nimport sys\n\nif sys.platform == \"win32\":\n from .windows import PtySession\nelse:\n from .unix import PtySession\n\n__all__ = [\"PtySession\"]\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pkg/_pty/unix.py\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```python\nfrom __future__ import annotations\n\nimport pty\n\nfrom .interface import PtySessionInterface\n\n\nclass PtySession(PtySessionInterface):\n def __init__(self, command: list[str]) -> None:\n super().__init__(command)\n\n self._fd, self._child_fd = pty.openpty()\n\n def get_exit_code(self) -> int | None:\n return None\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\n\u003ctable>\n\u003ctr>\u003cth>\u003ccode>pkg/_pty/windows.py\u003c/code>\u003c/th>\u003c/tr>\n\u003ctr>\u003ctd>\n\n```python\nfrom __future__ import annotations\n\nfrom .interface import PtySessionInterface\n\n\nclass PtySession(PtySessionInterface):\n def __init__(self, command: list[str]) -> None:\n super().__init__(command)\n\n self._fd, self._child_fd = (0, 0)\n\n def get_exit_code(self) -> int | None:\n return None\n```\n\n\u003c/td>\u003c/tr>\u003c/table>\n\nFinally, enter the `mypy-issue` directory and run `mypy pkg` on Windows:\n\n```\n$ mypy pkg\npkg\\_pty\\unix.py:12: error: Module has no attribute \"openpty\" [attr-defined]\n self._fd, self._child_fd = pty.openpty()\n ^~~~~~~~~~~\nFound 1 error in 1 file (checked 7 source files)\n```\n\n**Expected Behavior**\n\nI would expect that Mypy respects the platform condition like it does in other circumstances I've experienced and is documented.\n\n**Your Environment**\n\n- Mypy version used: mypy 1.15.0 (compiled: yes)\n- Python version used: 3.12","author":{"url":"https://github.com/ofek","@type":"Person","name":"ofek"},"datePublished":"2025-05-01T16:46:09Z","url":"https://github.com/python/mypy/issues/19009"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d40dd63a-1277-22a7-3529-8d84f910c46e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9454:144E47:2381BFF:2E1B411:6AFFEAD2 |
| html-safe-nonce | eed7c8d588021ecf669ab1adfb46b61da9c73906697f39e4861c87e5db91ccd1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDU0OjE0NEU0NzoyMzgxQkZGOjJFMUI0MTE6NkFGRkVBRDIiLCJ2aXNpdG9yX2lkIjoiNjM4NjEyMDY2MTk3ODI2MjU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7f9a80b341049d0aacfaf657473faff528ac31b5c2e1b739c8a04089562217a4 |
| hovercard-subject-tag | issue:3034246843 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/19009/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1e0c439e6e312168d5f6e6737ea59535fa16f9bab6c141eebae3a6ae93253aab/python/mypy/issues/19009 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1e0c439e6e312168d5f6e6737ea59535fa16f9bab6c141eebae3a6ae93253aab/python/mypy/issues/19009 |
| og:image:alt | Bug Report When you have the following provider module: if sys.platform == "win32": from my_pkg._pty.windows import PtySession else: from my_pkg._pty.unix import PtySession and attempt importing th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ofek |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width