Title: is_darwin is always False (os.name is never "darwin") · Issue #1731 · gitpython-developers/GitPython · GitHub
Open Graph Title: is_darwin is always False (os.name is never "darwin") · Issue #1731 · gitpython-developers/GitPython
X Title: is_darwin is always False (os.name is never "darwin") · Issue #1731 · gitpython-developers/GitPython
Description: As discussed in #1679 and #1679 (review), code inside or outside of GitPython that uses is_win from git.compat is prone to bugs, because it is easy to assume wrongly that is_win would be true on Cygwin. Instead, is_win is short for os.na...
Open Graph Description: As discussed in #1679 and #1679 (review), code inside or outside of GitPython that uses is_win from git.compat is prone to bugs, because it is easy to assume wrongly that is_win would be true on Cy...
X Description: As discussed in #1679 and #1679 (review), code inside or outside of GitPython that uses is_win from git.compat is prone to bugs, because it is easy to assume wrongly that is_win would be true on Cy...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/1731
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"is_darwin is always False (os.name is never \"darwin\")","articleBody":"As discussed in #1679 and https://github.com/gitpython-developers/GitPython/pull/1679#pullrequestreview-1644389896, code inside or outside of GitPython that uses `is_win` from `git.compat` is prone to bugs, because it is easy to assume wrongly that `is_win` would be true on Cygwin. Instead, `is_win` is short for `os.name == \"nt\"`, and writing that out makes clearer what one is testing.\r\n\r\nHowever, I have discovered that the potential for confusion in using the `is_\u003cplatform\u003e` aliases in `git.compat` is not limited to `is_win`. Specifically, **`is_darwin` is always `False`, including on macOS systems**.\r\n\r\nThis is because `is_darwin` is short for `os.name == \"darwin\"`, but `\"darwin\"` [is not currently](https://docs.python.org/3/library/os.html#os.name) and [seems never to have been](https://docs.python.org/2.7/library/os.html#os.name) one of the possible values of `os.name`. In Python 2, there were more values of `os.name` than in Python 3, but in both languages `os.name` is always `\"posix\"` on the operating system today called macOS (historically called Mac OS X, with Mac OS 9 and earlier not being POSIX systems).\r\n\r\nIn the infrequent case that one wishes to test for macOS, one can check `sys.platform == \"darwin\"`, because `sys.platform` is more granular than `os.name` and does take on the value of `\"darwin\"` on macOS. GitPython does not currently need to do this, and no uses of `is_darwin` appear in `git/` or `test/` currently. This appears to have been the case since 4545762 (#1295).\r\n\r\nThe buggy definition of `is_darwin` seems to have had limited impact in the project, having been introduced in f495e94 (part of #519) and only used in a couple of places, then none since #1295. Before #519, the correct expression `sys.platform == 'darwin'` was used, but it seems to have been inadvertently changed to the always-false `os.name == 'darwin'` [in that commit](https://github.com/gitpython-developers/GitPython/commit/f495e94028bfddc264727ffc464cd694ddd05ab8#diff-2f2b548c14a71e5bbf15502f6d7fd98a50842119152c451ae2ec5e1cc42f02d2). (The `is_\u003cplatform\u003e` attributes were soon after changed from functions to variables/constants in e61439b, which was also in #519.)\r\n\r\n### Possible fixes\r\n\r\nAlthough GitPython doesn't use `is_darwin` anymore, it is a public name in `git.compat` (since that module does not define `__all__` and the name `is_darwin` neither starts with a `_` nor has been documented as nonpublic). So it may be in other projects that use GitPython. If so, those other projects suffer either from this bug, or if not then from another bug of their own where they depend on `is_darwin` having the wrong value.\r\n\r\nBecause removing it would be a breaking change, it should probably be kept, but fixed to use the expression `sys.platform == \"darwin\"`. However, this does have the disadvantage that a developer who checks its implementation may assume `is_win` and `is_posix` are analogous, examining `sys.platform`. In the case of `is_win`, which checks `os.name = \"nt\"`, I believe it is currently equivalent to checking `sys.platform == \"win32\"`, though historically this may not always have been the case. In the case of `is_posix`, however, it is not equivalent to any straightforward expression with `sys.platform`, because a few \"POSIX\" platforms are distinguished with custom values of `sys.platform`.\r\n\r\nBoth for this reason and, more so, because of the confusion that turns out to have arisen from the `is_\u003cplatform\u003e` aliases, I recommend all of them be replaced, and also that they be documented as deprecated. Because deprecation is often not readily discovered when one is not looking for it--even if `DeprecationWarning` is emitted, that warning is hidden by default--I think `is_darwin` should probably be fixed to `sys.platform == \"darwin\"` as well, in spite of the disadvantages of doing so.\r\n\r\nA further benefit of replacing all uses is that static type checkers (and some editors) understand checks against standard-library attributes such as `os.name` and they are able to check code more precisely based on them. This helps with things like flags in the `subprocess` module that differ across platforms (though attribute accesses based on them have to be written across separate expressions, rather than for example in a ternary expression, to get the full benefit of these distinctions, at least with current type checkers). As far as I know, there's no fundamental reason this shouldn't also work with module attributes assigned that kind of expression (typing it automatically as a literal value), but that does not seem to happen.\r\n\r\nI've opened #1732 for this. For the reasons given below, I haven't included any code to emit new `DeprecationWarning`s, though I'd be pleased to do so if requested.\r\n\r\n### New warnings?\r\n\r\nAlthough I recommend the `is_\u003cplatform\u003e` attributes be deprecated, I am unsure if it is worthwhile to have accesses to those attributes raise any warnings at this time. There are a few considerations:\r\n\r\n- This can be done by defining a module-level `__getattr__`, but I think that slightly decreases the benefits of static typing. It can also be done by writing a class that inherits from `types.ModuleType` and defines properties, and assigning that class to the `__class__` attribute of the module object. This may be preferable, *if* it turns out to play better with static typing. See [Customizing module attribute access](https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access) (though it doesn't discuss static typing implications).\r\n- It seems very likely that `DeprecationWarning`s on particular module-level attribute accesses will be of use elsewhere in the project. (One possible place is for `__version__` as discussed in [this](https://github.com/gitpython-developers/GitPython/issues/1716#issuecomment-1773728098) and [that](https://github.com/gitpython-developers/GitPython/issues/1716#issuecomment-1773746757) comment, but I think there may be a number of others, too.) But I'd be more comfortable introducing either of these things once static typing is in better shape, so their effects on that can be better judged.\r\n- There is also a more specific and, in my view, important reason to be reluctant to add this functionality for those attributes right now: they are in `git.compat`, which could perhaps be *entirely* deprecated sometime soon. That module's original purpose was to help in supporting both Python 2 and Python 3. Since GitPython currently only supports Python 3, it might be possible to eliminate use of *all* of the facilities in `git.compat`, though whether or not that *should* be done is another question, since some uses of it might be considered to improve clarity for reasons not tied to the distinction between Python 2 and Python 3. But if `git.compat` as a whole is deprecated, then emitting a `DeprecationWarning` won't require any special attribute handling, as it can just be done with a top-level `warnings.warn` call in the module.","author":{"url":"https://github.com/EliahKagan","@type":"Person","name":"EliahKagan"},"datePublished":"2023-11-04T21:20:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1731/GitPython/issues/1731"}
| 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:c78147f9-6447-da2c-eae7-5501768c3041 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A63A:6A14:34F7FB1:35A18F4:69684F12 |
| html-safe-nonce | 821e1ddbd9f0c3d54d69b8c4b39a35f072c5fcf355e872b5fba26e475b752894 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjNBOjZBMTQ6MzRGN0ZCMTozNUExOEY0OjY5Njg0RjEyIiwidmlzaXRvcl9pZCI6Ijc1NTk0MTQzMTQxNTgyODA0NjYiLCJyZWdpb25fZWRnZSI6InNlYSIsInJlZ2lvbl9yZW5kZXIiOiJzZWEifQ== |
| visitor-hmac | c54da2388e2437264be0c63aec748bda3ab7f370f2a2aa617bcf7f0700f1fc7a |
| hovercard-subject-tag | issue:1977515389 |
| 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/gitpython-developers/GitPython/1731/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dc74acd80cef8d5fadd4461402222cd8f2c7d4e28ba33615120890118c885c91/gitpython-developers/GitPython/issues/1731 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dc74acd80cef8d5fadd4461402222cd8f2c7d4e28ba33615120890118c885c91/gitpython-developers/GitPython/issues/1731 |
| og:image:alt | As discussed in #1679 and #1679 (review), code inside or outside of GitPython that uses is_win from git.compat is prone to bugs, because it is easy to assume wrongly that is_win would be true on Cy... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EliahKagan |
| hostname | github.com |
| expected-hostname | github.com |
| None | f16c57f41ed243e5b4dfe9b9bcd6828bd83080b1b6dbb4ff239bbe9745f12e0c |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| 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 | cfa7062cc6d4fe8fcb156bd33f4c97bd3b2470af |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width