17 Commits

Author SHA1 Message Date
Satwik Kansal
83078d0d18 Merge pull request #372 from nifadyev/#371/setup-ci-to-lint-only-changed-loc
#371: Create temp file with changed LOC for lint CI action
2025-03-02 10:31:47 +05:30
Satwik Kansal
f03e111188 Merge remote-tracking branch 'origin/master' into dev 2025-03-02 10:31:30 +05:30
Vadim Nifadev
e374a889e2 Setup markdownlint Github Action 2025-02-28 09:05:27 +03:00
Satwik Kansal
c909ede235 Merge pull request #367 from nifadyev/translations/364/ru-fix-logical-typo
#364: Fix logical typo in section How not to use `is` operator
2025-01-16 23:48:13 +05:30
Vadim Nifadev
de24d18af5 Fix logical typo in section How not to use operator, Russian translation 2025-01-14 11:54:28 +03:00
Satwik Kansal
ceec5fddb9 Merge pull request #360 from nifadyev/fix/#269/fix-pr-template-path
Rename common PR template, fix links in PR and issue templates
2024-11-26 21:26:16 +05:30
Vadim Nifadev
684650beec Rename common PR template, fix links in PR and issue templates 2024-11-26 16:05:18 +03:00
Satwik Kansal
9c24ca961c Merge pull request #359 from nifadyev/fix/#269/change-issues-link-and-fix-typos
#269: Change issues link and fix typos in Github issue templates
2024-11-26 14:39:44 +05:30
Vadim Nifadev
84e757692b #269: Change issues link and fix typos in Github issue templates 2024-11-26 09:10:27 +03:00
Satwik Kansal
af072271cc Merge pull request #353 from nifadyev/#350/add-nox-support
#350: Add Nox and basic pyproject.toml created by Poetry
2024-11-05 16:41:12 +05:30
Vadim Nifadev
f956856d4c Merge branch 'dev' into #350/add-nox-support 2024-11-05 14:07:12 +03:00
Satwik Kansal
15429d083b Merge pull request #354 from nifadyev/feature/#347/configure-markdown-linter-and-formatter
#347: Add markdownlint-cli2 as Github Action and pre-commit hook
2024-11-05 15:38:43 +05:30
Vadim Nifadev
77c978b0f8 Fix Markdown syntax in README using Markdownlintcli autofix 2024-10-31 15:54:06 +03:00
Vadim Nifadev
8987d66adb Add Markdownlintcli config 2024-10-31 15:53:24 +03:00
Vadim Nifadev
f25156107b Configure Nox and basic Poetry support
- Use new structure for code blocks in README
2024-10-24 07:53:10 +03:00
Vadim Nifadev
c0217f29f3 Gix Github Action file extension 2024-10-23 10:21:52 +03:00
Vadim Nifadev
a999ed789f Add markdownlint-cli2 as Github Action and pre-commit hook 2024-10-23 08:54:18 +03:00
17 changed files with 856 additions and 368 deletions

25
.github/ISSUE_TEMPLATE/bug.md vendored Normal file
View File

@@ -0,0 +1,25 @@
---
name: Bug report
about: Report a problem and provide necessary context
title: 'Fix ...'
labels: 'bug'
---
<!--
Hi, thanks for submitting a bug. We appreciate that.
But, we will need some information about what's wrong to help you.
-->
## What's wrong
<!-- Describe what is not working. -->
## How it should work?
<!-- Describe how it should work. -->
## Checklist before calling for maintainers
* [ ] Have you checked to ensure there aren't other open [Issues](../issues) for the same problem?

23
.github/ISSUE_TEMPLATE/new_snippet.md vendored Normal file
View File

@@ -0,0 +1,23 @@
---
name: New snippet
about: Suggest new gotcha and try to explain it
title: 'New snippet: ...'
labels: 'new snippets'
---
<!--
Hi, thanks for submitting a new snippet. We appreciate that.
But, we will need some information and (optionally) explanation to accept it.
-->
## Description
## Snippet preview
## Checklist before calling for maintainers
* [ ] Have you checked to ensure there aren't other open [Issues](../issues) for the same update/change?
* [ ] Have you checked that this snippet is not similar to any of the existing snippets?
<!--Explanation is optional. You may suggest a snippet without deep understanding of its behavior.-->
* [ ] Have you added an `Explanation` section? It shall include the reasons for changes and why you'd like us to include them

13
.github/ISSUE_TEMPLATE/translation.md vendored Normal file
View File

@@ -0,0 +1,13 @@
---
name: Translation
about: Request a new traslation and start working on it (if possible)
title: 'Translate to ...'
labels: 'translations'
---
<!--Hi, thanks for suggesting a new translation. We appreciate that.-->
## Checklist before calling for maintainers
* [ ] Have you checked to ensure there aren't other open [Issues](../issues) for the same translation?
* [ ] Do you wish to make a translation by yourself?

13
.github/PULL_REQUEST_TEMPLATE/common.md vendored Normal file
View File

@@ -0,0 +1,13 @@
## #(issue number): Summarize your changes
<!--Please include the reasons behind these changes and any relevant context.
This project only accepts pull requests related to open issues -->
<!--- Special phrase to auto-close the issue that your PR fixes -->
Closes # (issue number)
## Checklist before requesting a review
- [ ] Have you followed the guidelines in [CONTRIBUTING.md](../CONTRIBUTING.md)?
- [ ] Have you performed a self-review?
- [ ] Have you added yourself into [CONTRIBUTORS.md](../CONTRIBUTORS.md)?

View File

@@ -0,0 +1,15 @@
## #(issue number): Summarize your changes
<!--- This project only accepts pull requests related to open issuesIf
Please discuss all questions in an issue first -->
<!--- Special phrase to auto-close the issue that your PR fixes -->
Closes # (issue number)
## Checklist before requesting a review
- [ ] Have you written simple and understandable explanation?
- [ ] Have you added new snippet into `snippets/` with suitable name and number?
- [ ] Have you updated Table of content? (later will be done by pre-commit)
- [ ] Have you followed the guidelines in [CONTRIBUTING.md](../CONTRIBUTING.md)?
- [ ] Have you performed a self-review?
- [ ] Have you added yourself into [CONTRIBUTORS.md](../CONTRIBUTORS.md)?

View File

@@ -0,0 +1,13 @@
## #(issue number): Translate to ...
<!--- This project only accepts pull requests related to open issues -->
<!--- Special phrase to auto-close the issue that your PR fixes -->
Closes # (issue number)
## Checklist before requesting a review
- [ ] Have you fetched the latest `master` branch?
- [ ] Have you translated all snippets?
- [ ] Have you followed the guidelines in [CONTRIBUTING.md](../CONTRIBUTING.md)?
- [ ] Have you performed a self-review?
- [ ] Have you added yourself into [CONTRIBUTORS.md](../CONTRIBUTORS.md)?

24
.github/workflows/pr.yml vendored Normal file
View File

@@ -0,0 +1,24 @@
on: [pull_request]
permissions:
contents: read
pull-requests: read
checks: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Write git diff to temp file
run: |
git fetch origin
git diff origin/${{ github.base_ref }} --unified=0 *.md translations/*/*.md \
> ${{ runner.temp }}/diff.md
- uses: DavidAnson/markdownlint-cli2-action@v17
with:
globs: "${{ runner.temp }}/diff.md"

17
.markdownlint.yaml vendored Normal file
View File

@@ -0,0 +1,17 @@
MD013:
line_length: 120
# no-duplicate-heading - Multiple headings with the same content (Ignore multiple `Explanation` headings)
MD024: false
# no-trailing-punctuation - Trailing punctuation in heading (Ignore exclamation marks in headings)
MD026: false
# no-inline-html : Inline HTML (HTML is used for centered and theme specific images)
MD033: false
# no-inline-html : Bare URL used (site should be attributed transparently, because otherwise we have to un-necesarily explain where the link directs)
MD034: false
# first-line-h1 : First line in a file should be a top-level heading (Ignore because diff file will never have valid heading)
MD041: false

7
.pre-commit-config.yaml vendored Normal file
View File

@@ -0,0 +1,7 @@
default_language_version:
python: python3.12
repos:
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.14.0
hooks:
- id: markdownlint-cli2

46
CONTRIBUTING.md vendored
View File

@@ -1,8 +1,19 @@
# Contributing
## Getting Started
Contributions are made to this repo via Issues and Pull Requests (PRs). A few general guidelines that cover both:
- Search for existing Issues and PRs before creating your own.
- We work hard to makes sure issues are handled in a timely manner but, depending on the impact, it could take a while to investigate the root cause. A friendly ping in the comment thread to the submitter or a contributor can help draw attention if your issue is blocking.
## Issues
All kinds of patches are welcome. Feel free to even suggest some catchy and funny titles for the existing Examples. The goal is to make this collection as interesting to read as possible. Here are a few ways through which you can contribute,
- If you are interested in translating the project to another language (some people have done that in the past), please feel free to open up an issue, and let me know if you need any kind of help.
- If you are interested in translating the project to another language, please feel free to open up an issue using `translation` template, and let me know if you need any kind of help.
- If the changes you suggest are significant, filing an issue before submitting the actual patch will be appreciated. If you'd like to work on the issue (highly encouraged), you can mention that you're interested in working on it while creating the issue and get assigned to it.
- If you're adding a new example, it is highly recommended to create an issue to discuss it before submitting a patch. You can use the following template for adding a new example:
- If you're adding a new example, it is highly recommended to create an issue using `new_snippet` template to discuss it before submitting a patch. You can use the following template for adding a new example:
<pre>
### ▶ Some fancy Title *
@@ -33,31 +44,22 @@ Probably unexpected output
```
</pre>
## Pull requests
Few things that you can consider while writing an example,
- If you are choosing to submit a new example without creating an issue and discussing, please check the project to make sure there aren't similar examples already.
- Try to be consistent with the namings and the values you use with the variables. For instance, most variable names in the project are along the lines of `some_string`, `some_list`, `some_dict`, etc. You'd see a lot of `x`s for single letter variable names, and `"wtf"` as values for strings. There's no strictly enforced scheme in the project as such, but you can take a glance at other examples to get a gist.
- Try to be as creative as possible to add that element of "surprise" in the setting up part of an example. Sometimes this may mean writing a snippet a sane programmer would never write.
- Also, feel free to add your name to the [contributors list](/CONTRIBUTORS.md).
**Some FAQs**
## Common questions
What is is this after every snippet title (###) in the README: <!-- Example ID: 30f1d3fc-e267-4b30-84ef-4d9e7091ac1a --->? Should it be added manually or can it be ignored when creating new snippets?
That's a random UUID, it is used to keep identify the examples across multiple translations of the project. As a contributor, you don't have to worry about behind the scenes of how it is used, you just have to add a new random UUID to new examples in that format.
Where should new snippets be added? Top/bottom of the section, doesn't ?
There are multiple things that are considered to decide the order (the dependency on the other examples, difficulty level, category, etc). I'd suggest simply adding the new example at the bottom of a section you find more fitting (or just add it to the Miscellaneous section). Its order will be taken care of in future revisions.
What's the difference between the sections (the first two feel very similar)?
The section "Strain your brain" contains more contrived examples that you may not really encounter in real life, whereas the section "Slippery Slopes" contains examples that have the potential to be encountered more frequently while programming.
Before the table of contents it says that markdown-toc -i README.md --maxdepth 3 was used to create it. The pip package markdown-toc does not contain either -i or --maxdepth flags. Which package is meant, or what version of that package?
Should the new table of contents entry for the snippet be created with the above command or created manually (in case the above command does more than only add the entry)?
We use the [markdown-toc](https://www.npmjs.com/package/markdown-toc) npm package to generate ToC. It has some issues with special characters though (I'm not sure if it's fixed yet). More often than not, I just end up inserting the toc link manually at the right place. The tool is handy when I have to big reordering, otherwise just updating toc manually is more convenient imo.
- What is is this after every snippet title (###) in the README: <!-- Example ID: 30f1d3fc-e267-4b30-84ef-4d9e7091ac1a --->? Should it be added manually or can it be ignored when creating new snippets?
- That's a random UUID, it is used to keep identify the examples across multiple translations of the project. As a contributor, you don't have to worry about behind the scenes of how it is used, you just have to add a new random UUID to new examples in that format.
- Where should new snippets be added? Top/bottom of the section, doesn't ?
- There are multiple things that are considered to decide the order (the dependency on the other examples, difficulty level, category, etc). I'd suggest simply adding the new example at the bottom of a section you find more fitting (or just add it to the Miscellaneous section). Its order will be taken care of in future revisions.
- What's the difference between the sections (the first two feel very similar)?
- The section "Strain your brain" contains more contrived examples that you may not really encounter in real life, whereas the section "Slippery Slopes" contains examples that have the potential to be encountered more frequently while programming.
- Before the table of contents it says that `markdown-toc -i README.md --maxdepth 3` was used to create it. The pip package `markdown-toc` does not contain neither `-i` nor `--maxdepth` flags. Which package is meant, or what version of that package? Should the new table of contents entry for the snippet be created with the above command or created manually (in case the above command does more than only add the entry)?
- `markdown-toc` will be replaced in the near future, follow the [issue](https://github.com/satwikkansal/wtfpython/issues/351) to check the progress.
- We use the [markdown-toc](https://www.npmjs.com/package/markdown-toc) npm package to generate ToC. It has some issues with special characters though (I'm not sure if it's fixed yet). More often than not, I just end up inserting the toc link manually at the right place. The tool is handy when I have to big reordering, otherwise just updating toc manually is more convenient imo.
If you have any questions feel free to ask on [this issue](https://github.com/satwikkansal/wtfpython/issues/269) (thanks to [@LiquidFun](https://github.com/LiquidFun) for starting it).

823
README.md vendored

File diff suppressed because it is too large Load Diff

13
noxfile.py Normal file
View File

@@ -0,0 +1,13 @@
from typing import TYPE_CHECKING
import nox
if TYPE_CHECKING:
from nox.sessions import Session
python_versions = ["3.9", "3.10", "3.11", "3.12", "3.13"]
@nox.session(python=python_versions, reuse_venv=True)
def tests(session: "Session") -> None:
_ = session.run("python", "snippets/2_tricky_strings.py")

155
poetry.lock generated vendored Normal file
View File

@@ -0,0 +1,155 @@
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
[[package]]
name = "argcomplete"
version = "3.5.1"
description = "Bash tab completion for argparse"
optional = false
python-versions = ">=3.8"
files = [
{file = "argcomplete-3.5.1-py3-none-any.whl", hash = "sha256:1a1d148bdaa3e3b93454900163403df41448a248af01b6e849edc5ac08e6c363"},
{file = "argcomplete-3.5.1.tar.gz", hash = "sha256:eb1ee355aa2557bd3d0145de7b06b2a45b0ce461e1e7813f5d066039ab4177b4"},
]
[package.extras]
test = ["coverage", "mypy", "pexpect", "ruff", "wheel"]
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
files = [
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
[[package]]
name = "colorlog"
version = "6.8.2"
description = "Add colours to the output of Python's logging module."
optional = false
python-versions = ">=3.6"
files = [
{file = "colorlog-6.8.2-py3-none-any.whl", hash = "sha256:4dcbb62368e2800cb3c5abd348da7e53f6c362dda502ec27c560b2e58a66bd33"},
{file = "colorlog-6.8.2.tar.gz", hash = "sha256:3e3e079a41feb5a1b64f978b5ea4f46040a94f11f0e8bbb8261e3dbbeca64d44"},
]
[package.dependencies]
colorama = {version = "*", markers = "sys_platform == \"win32\""}
[package.extras]
development = ["black", "flake8", "mypy", "pytest", "types-colorama"]
[[package]]
name = "distlib"
version = "0.3.9"
description = "Distribution utilities"
optional = false
python-versions = "*"
files = [
{file = "distlib-0.3.9-py2.py3-none-any.whl", hash = "sha256:47f8c22fd27c27e25a65601af709b38e4f0a45ea4fc2e710f65755fa8caaaf87"},
{file = "distlib-0.3.9.tar.gz", hash = "sha256:a60f20dea646b8a33f3e7772f74dc0b2d0772d2837ee1342a00645c81edf9403"},
]
[[package]]
name = "filelock"
version = "3.16.1"
description = "A platform independent file lock."
optional = false
python-versions = ">=3.8"
files = [
{file = "filelock-3.16.1-py3-none-any.whl", hash = "sha256:2082e5703d51fbf98ea75855d9d5527e33d8ff23099bec374a134febee6946b0"},
{file = "filelock-3.16.1.tar.gz", hash = "sha256:c249fbfcd5db47e5e2d6d62198e565475ee65e4831e2561c8e313fa7eb961435"},
]
[package.extras]
docs = ["furo (>=2024.8.6)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4.1)"]
testing = ["covdefaults (>=2.3)", "coverage (>=7.6.1)", "diff-cover (>=9.2)", "pytest (>=8.3.3)", "pytest-asyncio (>=0.24)", "pytest-cov (>=5)", "pytest-mock (>=3.14)", "pytest-timeout (>=2.3.1)", "virtualenv (>=20.26.4)"]
typing = ["typing-extensions (>=4.12.2)"]
[[package]]
name = "nox"
version = "2024.10.9"
description = "Flexible test automation."
optional = false
python-versions = ">=3.8"
files = [
{file = "nox-2024.10.9-py3-none-any.whl", hash = "sha256:1d36f309a0a2a853e9bccb76bbef6bb118ba92fa92674d15604ca99adeb29eab"},
{file = "nox-2024.10.9.tar.gz", hash = "sha256:7aa9dc8d1c27e9f45ab046ffd1c3b2c4f7c91755304769df231308849ebded95"},
]
[package.dependencies]
argcomplete = ">=1.9.4,<4"
colorlog = ">=2.6.1,<7"
packaging = ">=20.9"
tomli = {version = ">=1", markers = "python_version < \"3.11\""}
virtualenv = ">=20.14.1"
[package.extras]
tox-to-nox = ["jinja2", "tox"]
uv = ["uv (>=0.1.6)"]
[[package]]
name = "packaging"
version = "24.1"
description = "Core utilities for Python packages"
optional = false
python-versions = ">=3.8"
files = [
{file = "packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"},
{file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"},
]
[[package]]
name = "platformdirs"
version = "4.3.6"
description = "A small Python package for determining appropriate platform-specific dirs, e.g. a `user data dir`."
optional = false
python-versions = ">=3.8"
files = [
{file = "platformdirs-4.3.6-py3-none-any.whl", hash = "sha256:73e575e1408ab8103900836b97580d5307456908a03e92031bab39e4554cc3fb"},
{file = "platformdirs-4.3.6.tar.gz", hash = "sha256:357fb2acbc885b0419afd3ce3ed34564c13c9b95c89360cd9563f73aa5e2b907"},
]
[package.extras]
docs = ["furo (>=2024.8.6)", "proselint (>=0.14)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4)"]
test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=8.3.2)", "pytest-cov (>=5)", "pytest-mock (>=3.14)"]
type = ["mypy (>=1.11.2)"]
[[package]]
name = "tomli"
version = "2.0.2"
description = "A lil' TOML parser"
optional = false
python-versions = ">=3.8"
files = [
{file = "tomli-2.0.2-py3-none-any.whl", hash = "sha256:2ebe24485c53d303f690b0ec092806a085f07af5a5aa1464f3931eec36caaa38"},
{file = "tomli-2.0.2.tar.gz", hash = "sha256:d46d457a85337051c36524bc5349dd91b1877838e2979ac5ced3e710ed8a60ed"},
]
[[package]]
name = "virtualenv"
version = "20.27.0"
description = "Virtual Python Environment builder"
optional = false
python-versions = ">=3.8"
files = [
{file = "virtualenv-20.27.0-py3-none-any.whl", hash = "sha256:44a72c29cceb0ee08f300b314848c86e57bf8d1f13107a5e671fb9274138d655"},
{file = "virtualenv-20.27.0.tar.gz", hash = "sha256:2ca56a68ed615b8fe4326d11a0dca5dfbe8fd68510fb6c6349163bed3c15f2b2"},
]
[package.dependencies]
distlib = ">=0.3.7,<1"
filelock = ">=3.12.2,<4"
platformdirs = ">=3.9.1,<5"
[package.extras]
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.2,!=7.3)", "sphinx-argparse (>=0.4)", "sphinxcontrib-towncrier (>=0.2.1a0)", "towncrier (>=23.6)"]
test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7)", "packaging (>=23.1)", "pytest (>=7.4)", "pytest-env (>=0.8.2)", "pytest-freezer (>=0.4.8)", "pytest-mock (>=3.11.1)", "pytest-randomly (>=3.12)", "pytest-timeout (>=2.1)", "setuptools (>=68)", "time-machine (>=2.10)"]
[metadata]
lock-version = "2.0"
python-versions = "^3.9"
content-hash = "af91619c8e62e649ee538e51a248ef2fc1e4f4495e7748b3b551685aa47b404e"

16
pyproject.toml vendored Normal file
View File

@@ -0,0 +1,16 @@
[tool.poetry]
name = "wtfpython"
version = "3.0.0"
description = "What the f*ck Python!"
authors = ["Satwik Kansal <discuss@satwikkansal.xyz>"]
license = "WTFPL 2.0"
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.9"
nox = "^2024.10.9"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

View File

@@ -0,0 +1,19 @@
# 1
assert id("some_string") == id("some" + "_" + "string")
assert id("some_string") == id("some_string")
# 2
a = "wtf"
b = "wtf"
assert a is b
a = "wtf!"
b = "wtf!"
assert a is b
# 3
a, b = "wtf!", "wtf!"
assert a is b
a = "wtf!"; b = "wtf!"
assert a is b

0
snippets/__init__.py Normal file
View File

View File

@@ -559,7 +559,7 @@ False
Интерпретатор не понимает, что до выполнения выражения `y = 257` целое число со значением `257` уже создано, и поэтому он продолжает создавать другой объект в памяти.
Подобная оптимизация применима и к другим **изменяемым** объектам, таким как пустые кортежи. Поскольку списки являются изменяемыми, поэтому `[] is []` вернет `False`, а `() is ()` вернет `True`. Это объясняет наш второй фрагмент. Перейдем к третьему,
Подобная оптимизация применима и к другим **неизменяемым** объектам, таким как пустые кортежи. Поскольку списки являются изменяемыми, поэтому `[] is []` вернет `False`, а `() is ()` вернет `True`. Это объясняет наш второй фрагмент. Перейдем к третьему,
**И `a`, и `b` ссылаются на один и тот же объект при инициализации одним и тем же значением в одной и той же строке**.